我正在尝试访问我编写的RESTful Web服务:
http://localhost:8080/dukegen/ws/family/1
Run Code Online (Sandbox Code Playgroud)
但是使用浏览器中的地址栏获取404并且不知道原因.我想恢复JSON.我把杰克逊2放在我的课堂上:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.3.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
这是服务器输出:
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'
Run Code Online (Sandbox Code Playgroud)
这是我的控制器:
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我在web.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>
Run Code Online (Sandbox Code Playgroud)
我的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>
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
Ano*_*oop 18
有些事情在这里不正确.
首先,在您的请求映射中,映射应该是一致的.您的类应该映射到, "/ws"
并且您的方法应该生成结果"/family/{familyId}"
在你的web.xml中,你已经配置了servlet来响应/ws/*
,你的控制器ws
再次被请求映射.这不会工作.
一旦"/ws/*"
被servlet截获,就不应该在请求映射中重复它.Controller仅响应其上下文中的URL模式.无论是后"/ws"
您的网址只在控制器的情况下.
我通常更喜欢将servlet映射到"/"
控制器内的所有其他分辨率.不过只是我的偏好.
所以正确的配置是
web.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>/</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
和控制器
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
22829 次 |
最近记录: |