java - Spring MVC Controller 无法解析 URI

标签 java spring rest spring-mvc

这与之前提出的问题类似,但仍然不同,我根本无法弄清楚。

我有一个用 MVC 实现的 REST Web 服务 Controller ,它应该处理路径“/users/contacts”的请求,问题是在我的客户端测试应用程序中,当我尝试访问该路径时,我得到:

No mapping found for HTTP request with URI /users/users/contacts in DispatcherServlet with name 'webservice'

同样,在我的客户端测试应用程序中,当我将请求路径更改为“/WHATEVERusers/contacts”之类的内容时,我会收到一条错误,指出它无法解析“/WHATEVERusers/contacts”

我不知道为什么当我尝试访问由 Controller 处理的路径时,它会被破坏,但当我请求一些垃圾路径时,它不会。

我面前没有代码,但回家后可以回答问题。

这是我的测试客户端的代码:

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 类:

@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>

更新: 我的请求在我的 Controller 中处理,但是在处理请求后,服务器打印出它无法解析/users/users/contacts。由于某种原因,另一个请求正在发送到 DispsatcherServlet,我不知道为什么..

谢谢, 标记

最佳答案

@ResponseBody 添加到我的方法签名解决了我的问题。没有意识到我实际上需要它,并且没有它会导致非常奇怪的副作用。

关于java - Spring MVC Controller 无法解析 URI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10402936/

相关文章:

java - 带有属性值请求映射的 Spring Boot REST Controller 测试

java - 有人将 Leonard Richardson 和 Sam Ruby 所著的 "RESTful Web Services"书中的 reSTLet 示例更新为 reSTLet 2.0 吗?

java - upheap 方法的递归函数(我如何将这个非递归方法写成递归的)

java - 使用 HANA 的 Web 应用程序,获取 SLF4J : Class path contains multiple SLF4J bindings

java - 解析json列表

java - Sprint 中的 DAO 模式是否适合从 PostgreSQL 切换到 ElasticSearch

java - 在 post api 的请求正文中接受数组时处理失败的更好方法?

rest - 最简单的服务器到服务器身份验证

java - 同步mysql数据库和文件夹

java - 如何等待第二个窗口完全加载