2015-12-15 109 views
0

我在這裏看到過其他類似的帖子在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

回答

1

你有定義爲在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) 

你在日誌中看到正在註冊的請求映射路徑?

+0

我改變了一些事情。從簡單的Web應用程序(而不是一個春季啓動項目) 它看起來像這樣 - https://jsfiddle.net/7t03hx3t/ 也使我的休息方法公開,顯然我可以打網絡服務現在。我正在得到另一個有關406不可接受的問題 如下所示 - https://jsfiddle.net/3h1Lg75h/ 但我會針對該問題發佈一個單獨的問題。謝謝你的幫助。 –