spring mvc4 http 404 找不到错误
spring mvc4 http 404 not found error
我在 Whosebug 上看到过其他类似的帖子 - 大多数情况下,发帖人所遇到的 link 与 RESTController 中的服务方法所解析的内容不同。我确定(在下面解释)我没有犯那个错误,但它仍然不适合我..
我正在为 Servlet 2.5 容器 (weblogic 10.3.6) 编写一个基于 Spring MVC4 的 REST 控制器,有人建议我对我的 Web 应用程序项目遵循此 sample web.xml
我已经关注了这个 web.xml 但我不确定本文中具有值(如下所示)的 contextAttribute 的作用..
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextAttribute</param-name>
<param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
当我 运行 我的网络应用程序并使用 REST 客户端访问它时,我不断收到 http 404 not found 错误。完整的错误发布在这里
http://paste.ubuntu.com/13966788/
这是错误片段
08:44:34.368 [main] DEBUG o.s.web.client.RestTemplate - GET request for "http://localhost:7001/demo-0.0.1-SNAPSHOT/customers/2" resulted in 404 (Not Found); inv
oking error handlerTests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.402 sec <<< FAILURE! - in demo.DemoApplicationTests
contextLoaded(demo.DemoApplicationTests) Time elapsed: 0.042 sec <<< ERROR!
org.springframework.web.client.HttpClientErrorException: 404 Not Found
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:641)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:597)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557)
at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:289)
at demo.DemoApplicationTests.contextLoaded(DemoApplicationTests.java:45)
我不确定我的上下文根应该是什么,weblogic 控制台中部署的 webapp 显示值 'demo-0.0.1-SNAPSHOT'
我的 REST 控制器方法的 @RequestMapping 是 /customers/{id}。
@RestController
public class CustomerRestController {
@Autowired
private CustomerRepository customerRepository;
@RequestMapping("/customers/{id}")
CustomerProtos.Customer customer(@PathVariable Integer id) {
return this.customerRepository.findById(id);
}
所以我假设我的 url 路径应该像
localhost:7001/demo-0.0.1-SNAPSHOT/customers/2
但是当我点击这个 URL 时(在浏览器和基于 spring 的测试客户端中)
我不断收到 http 404 未找到错误。
这是我的 spring 测试客户端..
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class DemoApplicationTests {
@Configuration
public static class RestClientConfiguration {
@Bean
RestTemplate restTemplate(ProtobufHttpMessageConverter hmc) {
return new RestTemplate(Arrays.asList(hmc));
}
@Bean
ProtobufHttpMessageConverter protobufHttpMessageConverter() {
return new ProtobufHttpMessageConverter();
}
}
@Autowired
private RestTemplate restTemplate;
@Test
public void contextLoaded() {
ResponseEntity<CustomerProtos.Customer> customer = restTemplate.getForEntity(
"http://localhost:7001/demo-0.0.1-SNAPSHOT/customers/2", CustomerProtos.Customer.class);
System.out.println("customer retrieved: " + customer.toString());
}
我想知道 contextAttribute 是否在此处执行某些操作。有什么想法吗?
我在这里做的唯一不同的事情是我注册了 Spring MVC4 的 ProtoBufHttpMessageConverter 而不是 httpMessageConverter。但在这种情况下不应该有所作为。
我的代码在 github 上共享 https://goo.gl/cVNEPT
你有没有在 web.xml 中定义的 servletMapping?
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
此外,public 用于您的方法
@RequestMapping("/customers/{id}")
public CustomerProtos.Customer customer(@PathVariable Integer id)
您是否在日志中看到正在注册的请求映射路径?
我在 Whosebug 上看到过其他类似的帖子 - 大多数情况下,发帖人所遇到的 link 与 RESTController 中的服务方法所解析的内容不同。我确定(在下面解释)我没有犯那个错误,但它仍然不适合我..
我正在为 Servlet 2.5 容器 (weblogic 10.3.6) 编写一个基于 Spring MVC4 的 REST 控制器,有人建议我对我的 Web 应用程序项目遵循此 sample web.xml
我已经关注了这个 web.xml 但我不确定本文中具有值(如下所示)的 contextAttribute 的作用..
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextAttribute</param-name>
<param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
当我 运行 我的网络应用程序并使用 REST 客户端访问它时,我不断收到 http 404 not found 错误。完整的错误发布在这里 http://paste.ubuntu.com/13966788/
这是错误片段
08:44:34.368 [main] DEBUG o.s.web.client.RestTemplate - GET request for "http://localhost:7001/demo-0.0.1-SNAPSHOT/customers/2" resulted in 404 (Not Found); inv
oking error handlerTests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.402 sec <<< FAILURE! - in demo.DemoApplicationTests
contextLoaded(demo.DemoApplicationTests) Time elapsed: 0.042 sec <<< ERROR!
org.springframework.web.client.HttpClientErrorException: 404 Not Found
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:641)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:597)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557)
at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:289)
at demo.DemoApplicationTests.contextLoaded(DemoApplicationTests.java:45)
我不确定我的上下文根应该是什么,weblogic 控制台中部署的 webapp 显示值 'demo-0.0.1-SNAPSHOT'
我的 REST 控制器方法的 @RequestMapping 是 /customers/{id}。
@RestController
public class CustomerRestController {
@Autowired
private CustomerRepository customerRepository;
@RequestMapping("/customers/{id}")
CustomerProtos.Customer customer(@PathVariable Integer id) {
return this.customerRepository.findById(id);
}
所以我假设我的 url 路径应该像
localhost:7001/demo-0.0.1-SNAPSHOT/customers/2
但是当我点击这个 URL 时(在浏览器和基于 spring 的测试客户端中) 我不断收到 http 404 未找到错误。
这是我的 spring 测试客户端..
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class DemoApplicationTests {
@Configuration
public static class RestClientConfiguration {
@Bean
RestTemplate restTemplate(ProtobufHttpMessageConverter hmc) {
return new RestTemplate(Arrays.asList(hmc));
}
@Bean
ProtobufHttpMessageConverter protobufHttpMessageConverter() {
return new ProtobufHttpMessageConverter();
}
}
@Autowired
private RestTemplate restTemplate;
@Test
public void contextLoaded() {
ResponseEntity<CustomerProtos.Customer> customer = restTemplate.getForEntity(
"http://localhost:7001/demo-0.0.1-SNAPSHOT/customers/2", CustomerProtos.Customer.class);
System.out.println("customer retrieved: " + customer.toString());
}
我想知道 contextAttribute 是否在此处执行某些操作。有什么想法吗?
我在这里做的唯一不同的事情是我注册了 Spring MVC4 的 ProtoBufHttpMessageConverter 而不是 httpMessageConverter。但在这种情况下不应该有所作为。
我的代码在 github 上共享 https://goo.gl/cVNEPT
你有没有在 web.xml 中定义的 servletMapping?
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
此外,public 用于您的方法
@RequestMapping("/customers/{id}")
public CustomerProtos.Customer customer(@PathVariable Integer id)
您是否在日志中看到正在注册的请求映射路径?