2017-08-16 93 views
0

我正在嘗試構建Restful Web服務。我的Maven項目名稱是其他我正在關注Spring's Building a RESTful Web Service這樣做,但我不想使用Spring Boot,只是創建一個war並將其託管在我的eclise/STS的Tomcat中。這裏是我的web.xml和XX-servlet.xml的文件:在運行Spring Restful web服務時沒有Spring Boot的問題

的web.xml

<web-app> 
    <display-name>Archetype Created Web Application</display-name> 

    <!-- The front controller of this Spring Web application, responsible for handling all application requests --> 
    <servlet> 
     <servlet-name>rest</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <!-- Map all requests to the DispatcherServlet for handling --> 
    <servlet-mapping> 
     <servlet-name>rest</servlet-name> 
     <url-pattern>/*</url-pattern> 
    </servlet-mapping> 
</web-app> 

休息-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:p="http://www.springframework.org/schema/p" 
     xmlns:security="http://www.springframework.org/schema/security" 
     xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
     http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 

     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 

     <context:annotation-config /> 
     <context:component-scan base-package="com.rest" /> 
     <mvc:annotation-driven/> 


</beans> 

GreetingController.java

@RestController 
public class GreetingController2 { 
    private final AtomicLong counter = new AtomicLong(); 

    @RequestMapping("/greeting") 
    public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) { 
     return new Greeting(counter.incrementAndGet(), String.format("Hello %s", name)); 
    } 
} 

當我在Tomcat上運行此URL時:http://localhost:8080/rest給出了404,我在tomcat控制檯Aug 16, 2017 3:59:28 PM org.springframework.web.servlet.PageNotFound noHandlerFound WARNING: No mapping found for HTTP request with URI [/rest/] in DispatcherServlet with name 'rest' 中看到此消息,而我確實有映射。

而當我點擊http://localhost:8080/rest/greeting時,我得到http 406,消息The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.,而根據教程,它應該轉換爲JSON,它可以在瀏覽器中呈現。

我花了很多時間試圖弄清楚,看着SO上的各種帖子,以找出什麼是錯的,但不能。

+0

阿克沙伊塔瓦的回答幫我找到這個問題。除了他提到的之外,我沒有Jackson JSON依賴性,這就是爲什麼它無法將我的對象轉換爲JSON。當我將它們添加到我的pom.xml中時,我可以看到http:// localhost:8080/rest/greeting正在工作。 – Learner

回答

3

你不必像
@RequestMapping(「/」)
根據您的代碼默認映射你上面應該有以下網址做工精細
http://localhost:8080/rest/greeting

+0

我得到了http406消息'由這個請求標識的資源只能根據請求「接受」標題生成不可接受的特徵響應.'對於http:// localhost:8080/rest/greeting – Learner

+0

添加所有的傑克遜JAR依賴關係,如果已經添加,然後添加接受頭作爲applciation/JSON。如果你使用chrome,那麼更喜歡使用postman rest客戶端,並將accept頭改爲application/json。傑克遜的依賴包括jackson-core,jackson-databing,jackson-annotations –

+0

我明白了,當我從我的方法返回一個String時,它會呈現它。所以是的,這是與將對象轉換爲JSON不起作用。我會嘗試你的建議。謝謝Akshay +1 – Learner

相關問題