spring mvc4 http 404找不到错误
我在这里看到过其他类似的帖子在stackoverflow上 - 大多数情况下海报打到的链接与RESTController中的服务方法解析为不同。我正在确保(下面解释)我没有犯这个错误,但仍然没有为我工作..spring mvc4 http 404找不到错误
我正在写一个基于Spring MVC4的REST控制器的Servlet 2.5容器(weblogic 10.3.6),我是建议关注该sample web.xml我的web应用程序的项目
我按照这个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>
当我我正在运行我的web应用程序,并用REST客户端打它,我不断得到HTTP 404未找到错误。完整的错误在这里发布 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控制台部署的Web应用程序显示值“演示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(无论是在浏览器和基于弹簧的测试客户端) 我不断收到HTTP 404未找到错误。
这里是我的春天测试客户端..
@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在这里做一些事情。有任何想法吗 ?
我在这里做的唯一另外一件事是用httpMessageConverter代替,我注册了Spring MVC4的ProtoBufHttpMessageConverter。但在这种情况下不应该有所作为。
我的代码共享(在github)这里https://goo.gl/cVNEPT
你有定义为在web.xml servletMapping?
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
此外,有市民你的方法
@RequestMapping("/customers/{id}")
public CustomerProtos.Customer customer(@PathVariable Integer id)
你在日志中看到正在注册的请求映射路径?
我改变了一些事情。从简单的Web应用程序(而不是一个春季启动项目) 它看起来像这样 - https://jsfiddle.net/7t03hx3t/ 也使我的休息方法公开,显然我可以打网络服务现在。我正在得到另一个有关406不可接受的问题 如下所示 - https://jsfiddle.net/3h1Lg75h/ 但我会针对该问题发布一个单独的问题。谢谢你的帮助。 –