这与之前提出的问题类似,但仍然不同,我根本无法弄清楚。
我有一个用 MVC 实现的 REST Web 服务的控制器,它应该处理对路径“/users/contacts”的请求,问题是在我的客户端测试应用程序中,当我尝试点击该路径时,我得到:
No mapping found for HTTP request with URI /users/users/contacts in DispatcherServlet with name 'webservice'
同样,在我的客户端测试应用程序中,当我将请求路径更改为“/WHATEVERusers/contacts”之类的内容时,我会收到无法解析“/WHATEVERusers/contacts”的错误
我不知道为什么当我尝试访问由控制器处理的路径时,它会被破坏,但是当我请求一些垃圾路径时,它不会。
我面前没有代码,但我回家后能够回答问题。
这是我的测试客户端的代码:
private static void TestGetContacts()
{
UserCreateRequest request = new UserCreateRequest();
Gson gson = new Gson();
try
{
HttpClient client = new DefaultHttpClient();
StringEntity string = new StringEntity(gson.toJson(request), HTTP.UTF_8);
string.setContentType("application/json");
URI uri = URIUtils.createURI("http", "localhost", 8888, "/users/contacts", null, null);
HttpPost post = new HttpPost(uri);
post.setEntity(string);
client.execute(post);
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
这是我的控制器类:
@Controller
@RequestMapping("/users/contacts")
public class UserContactService
{
private @Autowired ApplicationContext appContext;
@RequestMapping(method=RequestMethod.POST, consumes="application/json", produces="application/json")
public GetContactsResponse GetContacts(@RequestBody UserCreateRequest request)
{
GetContactsResponse response = new GetContactsResponse();
return response;
}
}
这是我的 servlet 配置:
<?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"
xmlns:oxm="http://www.springframework.org/schema/oxm"
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
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">
<!--
- The controllers are autodetected POJOs labeled with the @Controller annotation.
-->
<context:component-scan base-package="com.impersonal.server.restservice.users"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
<!--
<ref bean="marshallingConverter" />
<ref bean="atomConverter" />
-->
</list>
</property>
</bean>
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
</bean>
这是我的网络配置:
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/**/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>webservice</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>webservice</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
更新: 我的请求在我的控制器中处理,但在处理请求后,服务器打印出它无法解析 /users/users/contacts。出于某种原因,另一个请求正在发送到 DispsatcherServlet,我不知道为什么..
谢谢,马克