2

我有一个 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 发送数据。谢谢。

4

1 回答 1

1

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

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

另外,如果您要传递一个字符串,我建议将一个简单的 http 字符串转换器添加到其余模板的转换器中,并使用@Consumes(text/plain). 这样你就可以把它当作一个简单的字符串。

于 2013-06-04T20:47:46.610 回答