关于rest:spring mvc4 http 404 not found错误

spring mvc4 http 404 not found error

我在stackoverflow上也看到过其他类似的帖子-多数张贴者所链接的链接与RESTController中的服务方法所解决的链接不同。我确定(如下所述)我没有在犯该错误,但仍然对我不起作用。.

我正在为Servlet 2.5容器(weblogic 10.3.6)编写基于Spring MVC4的REST控制器,并建议为我的Web应用程序项目遵循此样本web.xml。

我遵循了这个web.xml,但是我不确定本文中带有值的contextAttribute(如下所示)的作用..

1
2
3
4
5
6
7
8
9
<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 not found错误。完整的错误发布在这里
http://paste.ubuntu.com/13966788/

这是错误代码段

1
2
3
4
5
6
7
8
9
10
    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}。

1
2
3
4
5
6
7
8
@RestController
public class CustomerRestController {
    @Autowired
    private CustomerRepository customerRepository;
@RequestMapping("/customers/{id}")
CustomerProtos.Customer customer(@PathVariable Integer id) {
    return this.customerRepository.findById(id);
}

所以我认为我的网址路径应类似于

1
localhost:7001/demo-0.0.1-SNAPSHOT/customers/2

但是当我点击此URL时(在浏览器和基于Spring的测试客户端中)
我不断收到http 404找不到错误。

这是我的Spring测试客户。.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@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上共享(在github上)https://goo.gl/cVNEPT


您是否已在web.xml中定义了servletMapping?

1
2
3
4
<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

此外,请公开您的方法

1
2
@RequestMapping("/customers/{id}")
public CustomerProtos.Customer customer(@PathVariable Integer id)

您是否看到请求映射路径已在日志中注册?