【问题标题】:Spring mvc RestTemplate postForObject doesn't send parameter to REST APISpring mvc RestTemplate postForObject 不向 REST API 发送参数
【发布时间】:2013-06-04 20:38:45
【问题描述】:

我有一个 REST API。我正在做一个客户。 我将 Spring Security 配置为通过我的休息服务进行身份验证。 我的 REST API 控制器

@RequestMapping(value = "/login", method = RequestMethod.POST, headers="content-type=application/json")
public @ResponseBody
UserDetails loginUser(WebRequest request)
        throws LoginOrPasswordException {
    logger.info("login user");
    logger.info(request);
    String username = request.getParameter("username");
    logger.info(username);
    UserDetails uDetails = userRepository.login(username);
    System.out.println(uDetails.getPassword());
    return uDetails;
}

API 的应用上下文

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

<mvc:annotation-driven />

<context:component-scan base-package="com.zayats." />

<bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <bean
                class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
        </list>
    </property>
</bean>

<!-- Http Json MessageConverter -->
<bean id="jsonConverter"
    class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    <property name="supportedMediaTypes" value="application/json" />
</bean>

<import resource="jdbc-config.xml" />

接下来是带有restTemplate的客户端方法

HashMap<String, String> params = new HashMap<String, String>();
    params.put("username", username);
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<?> httpEntity = new HttpEntity<Object>(params, requestHeaders);
    logger.info("Create map and doing request.");
    UserDetails matchingUser = restTemplate.postForObject(
            "http://localhost:8080/ApiFamilyShoplist/user/login", httpEntity,
            UserDetails.class);

休息模板配置

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
    <property name="messageConverters">
        <list>
            <bean id="jsonConverter"
                class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="supportedMediaTypes" value="application/json" />
            </bean>
        </list>
    </property>
</bean>

当我在 API 控制器中使用 postForObject 调用我的 API 时,没有收到任何参数。 请给我一些建议,如何让它向 API 发送数据。 谢谢。

【问题讨论】:

    标签: spring rest post spring-mvc resttemplate


    【解决方案1】:

    您需要在您的方法中有另一个参数来接受字符串参数并使用@RequestBody(在方法签名内)对其进行注释:

    @ResponseBody
    @Consumes("text/plain")
    public UserDetails loginUser(WebRequest request, @RequestBody String userName)
    

    另外,如果你要传递一个字符串,我建议在 rest 模板的转换器中添加一个简单的 http 字符串转换器,并使用@Consumes(text/plain) 注释服务器方法。这样你就可以把它当作一个简单的字符串。

    【讨论】:

    • @RequestBody 用什么方法?在 API 控制器中?
    • @user2391060 您正在尝试获取请求参数(POST 或 GET),但是您正在发布 JSON。 HttpServletRequest 实体上不会有参数。所以你需要告诉 Spring 它需要将请求体转换为 JSON 对象。 @RequestBody Map&lt;String, Object&gt; params 应该是你的控制器方法的参数。
    • 我认为发送 JSON 并没有什么问题……建议使用字符串是一个相当激进的解决方案。对于字符串,您可以只使用请求参数。
    • @PavelHoral 说的也行。但是,您将获得映射到对象(实际参数)的键(参数名称)映射,而不是简单的字符串。如果我理解正确的话。
    • @Avi - 只是猜测 Jackson 能够解组为 Map&lt;String, Object&gt;。如果没有,则需要一个不同的类(例如ObjectNode
    猜你喜欢
    • 1970-01-01
    • 2012-07-19
    • 1970-01-01
    • 2018-02-19
    • 2017-07-10
    • 1970-01-01
    • 1970-01-01
    • 2019-03-20
    • 1970-01-01
    相关资源
    最近更新 更多