5
獲得404我試圖訪問我寫了一個RESTful Web服務:上簡單的Spring 4 REST服務
http://localhost:8080/dukegen/ws/family/1
但用在瀏覽器地址欄中得到一個404,不知道是什麼原因。我正在嘗試返回JSON。我已經把傑克遜2在我的類路徑:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.3.1</version>
</dependency>
這裏是服務器輸出:
Jan 14, 2014 8:29:55 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/ws/family/{familyId}] onto handler 'familyResource'
Jan 14, 2014 8:29:55 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/ws/family/{familyId}.*] onto handler 'familyResource'
Jan 14, 2014 8:29:55 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/ws/family/{familyId}/] onto handler 'familyResource'
Jan 14, 2014 8:29:55 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization completed in 360 ms
Jan 14, 2014 8:29:55 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/dukegen/ws/family/1] in DispatcherServlet with name 'dispatcher'
這裏是我的控制器:
@Controller
@RequestMapping("ws")
public class FamilyResource {
@RequestMapping(value="family/{familyId}", method = RequestMethod.GET, produces="application/json")
public @ResponseBody Family getFamily(@PathVariable long familyId) {
.... builds Family object ....
return family;
}
}
這裏是我的調度員在網路設定。 xml:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/mvcContext.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
我的mvcContext.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
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.xsd">
<context:component-scan base-package="ws.hamacher.dukegen.resource"/>
</beans>
任何幫助將不勝感激。
這個工作。我很感激。 – badgerduke
優秀的答案。 – David
我有一個我想要託管的靜態文件,但在匹配所有內容的url模式中有一個斜槓阻止了我這樣做。所以我不得不使用不同的URL映射,比如'ws'。謝謝。 –