Spring 总是 REST return 404
Spring REST always return 404
我使用 spring mvc 创建一个 rest web 服务。但是,我总是得到 404。我不知道哪里出错了。
这是我的初始化程序:
public class Initializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
// TODO Auto-generated method stub
return new Class<?>[] { RootConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
// TODO Auto-generated method stub
return new Class<?>[] { WebConfig.class };
}
@Override
protected String[] getServletMappings() {
// TODO Auto-generated method stub
return new String[] { "/" };
}
}
这是我的网络配置:
@Configuration
@EnableWebMvc
@ComponentScan("com.jh.dummy.web")
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResovler = new InternalResourceViewResolver();
viewResovler.setPrefix("/");
viewResovler.setSuffix(".html");
viewResovler.setExposeContextBeansAsAttributes(true);
return viewResovler;
}
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
这是我的其余服务器文件:
@RestController
@RequestMapping("/v1/")
public class PositionService {
@RequestMapping(value = "position", consumes = MediaType.APPLICATION_JSON_VALUE, method = POST)
public void create(List<Map<String, String>> data) {
}
@RequestMapping(value = "position/{id:\d+}", produces = MediaType.APPLICATION_JSON_VALUE, method = GET)
public void getById(@PathVariable("id") Long id) {
}
@RequestMapping(value = "position/title/{title:.+}/{start:\d{1,}}", produces = MediaType.APPLICATION_JSON_VALUE, method = GET)
public void getByTitle(@PathVariable("title") String title,
@PathVariable("start") int start) {
}
@RequestMapping(value = "position/title/{title:.+}/address/{address:\w{1,}}/{start:\d{1,}}", produces = MediaType.APPLICATION_JSON_VALUE, method = GET)
public void getByTitleAndLocation(@PathVariable("title") String title,
@PathVariable("address") String address, @PathVariable("start") int start) {
}
@RequestMapping(value = "position/address/{address:\w{1,}}/{start:\d{1,}}", produces = MediaType.APPLICATION_JSON_VALUE, method = GET)
public void getByLocation(@PathVariable("address") String address,
@PathVariable("start") int start) {
}
@RequestMapping(value = "position/company/{company:.{1,}}/{start:\d{1,}}", produces = MediaType.APPLICATION_JSON_VALUE, method = GET)
public void getByCompany(@PathVariable("company") String company,
@PathVariable("start") int start) {
}
}
请求如下:
我用curl测试的时候总是404,找不到哪里出错了。有什么想法吗?
我解决了问题。原来我需要将 /dummy/ 添加到 url 中。因此 url 将是 "localhost:8080/dummy/v1/position/v1"。但在我的其他项目案例中,我不需要将项目名称添加到虚拟项目中。我仍然不知道为什么这次我需要这样做。当我发现时,我会及时通知你。
我使用 spring mvc 创建一个 rest web 服务。但是,我总是得到 404。我不知道哪里出错了。
这是我的初始化程序:
public class Initializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
// TODO Auto-generated method stub
return new Class<?>[] { RootConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
// TODO Auto-generated method stub
return new Class<?>[] { WebConfig.class };
}
@Override
protected String[] getServletMappings() {
// TODO Auto-generated method stub
return new String[] { "/" };
}
}
这是我的网络配置:
@Configuration
@EnableWebMvc
@ComponentScan("com.jh.dummy.web")
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResovler = new InternalResourceViewResolver();
viewResovler.setPrefix("/");
viewResovler.setSuffix(".html");
viewResovler.setExposeContextBeansAsAttributes(true);
return viewResovler;
}
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
这是我的其余服务器文件:
@RestController
@RequestMapping("/v1/")
public class PositionService {
@RequestMapping(value = "position", consumes = MediaType.APPLICATION_JSON_VALUE, method = POST)
public void create(List<Map<String, String>> data) {
}
@RequestMapping(value = "position/{id:\d+}", produces = MediaType.APPLICATION_JSON_VALUE, method = GET)
public void getById(@PathVariable("id") Long id) {
}
@RequestMapping(value = "position/title/{title:.+}/{start:\d{1,}}", produces = MediaType.APPLICATION_JSON_VALUE, method = GET)
public void getByTitle(@PathVariable("title") String title,
@PathVariable("start") int start) {
}
@RequestMapping(value = "position/title/{title:.+}/address/{address:\w{1,}}/{start:\d{1,}}", produces = MediaType.APPLICATION_JSON_VALUE, method = GET)
public void getByTitleAndLocation(@PathVariable("title") String title,
@PathVariable("address") String address, @PathVariable("start") int start) {
}
@RequestMapping(value = "position/address/{address:\w{1,}}/{start:\d{1,}}", produces = MediaType.APPLICATION_JSON_VALUE, method = GET)
public void getByLocation(@PathVariable("address") String address,
@PathVariable("start") int start) {
}
@RequestMapping(value = "position/company/{company:.{1,}}/{start:\d{1,}}", produces = MediaType.APPLICATION_JSON_VALUE, method = GET)
public void getByCompany(@PathVariable("company") String company,
@PathVariable("start") int start) {
}
}
请求如下:
我用curl测试的时候总是404,找不到哪里出错了。有什么想法吗?
我解决了问题。原来我需要将 /dummy/ 添加到 url 中。因此 url 将是 "localhost:8080/dummy/v1/position/v1"。但在我的其他项目案例中,我不需要将项目名称添加到虚拟项目中。我仍然不知道为什么这次我需要这样做。当我发现时,我会及时通知你。