首页 文章

使用JSon进行RestTemplate POST

提问于
浏览
0

我再次寻求专家的帮助/指导,

我的问题是,我需要创建动态网站,调用restfull服务器获取数据,所有请求都是POST并返回json对象 . 我想使用Spring RestTemplate来调用服务器 . 我的服务器工作正常,这意味着目前一些Android和Apple应用程序连接到相同的API,它们工作正常 . 但是当我尝试使用RestTemplate连接到服务器时,它会产生一些错误

org.springframework.web.client.HttpClientErrorException:400 Bad Request

这是我的服务器,

@Controller
public class ABCController
{

     @RequestMapping(method = RequestMethod.POST, value = "/user/authenticate")
     public @ResponseBody LoginResponse login(@RequestParam("email") String email,@RequestParam("password") String password,@RequestParam("facebookId") String facebookId) {

        LoginRequest request = new LoginRequest(email, password, facebookId);

        UserBusiness userBusiness = UserBusinessImpl.getInstance();

        return userBusiness.login(request);

    }
}

这是我的服务器的 Spring 天配置,

<?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:mvc="http://www.springframework.org/schema/mvc"
        xmlns:jee="http://www.springframework.org/schema/jee"
       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.1.xsd                        http://www.springframework.org/schema/mvc                           http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/jee                          http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">  


    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

    <bean id="jsonViewResolver" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
    <bean id="viewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver" />


    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jsonConverter" />
            </list>
        </property>
    </bean>

    <bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
    <property name="supportedMediaTypes" value="application/json" />
    </bean> 


    <bean name="abcController" class="com.abc.def.controller.ABCController" />  

    <mvc:annotation-driven />
</beans>

这就是我尝试使用RestTemplate调用服务器的方法,

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
    <!-- <constructor-arg ref="httpClientFactory"/> -->

    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
               <property name="objectMapper">
                    <ref bean="JacksonObjectMapper" />
               </property>
            </bean>

        </list>
    </property>
</bean>

    <bean id="JacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper" />

这就是我如何使用它(用于测试)

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("root-context.xml");
     RestTemplate twitter = applicationContext.getBean("restTemplate", RestTemplate.class);

     MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
    map.add("email", "x1@test.com");
     map.add("password", "abc");
     map.add("facebookId", null);

    HttpEntity<LoginResponse> response= twitter.exchange("https://abc.com/Rest/api/user/authenticate", HttpMethod.POST, map, LoginResponse.class);

我的登录响应类及其子类,

  • LoginResponse

公共类LoginResponse扩展BaseResponse {
私有LoginBase数据;与吸气剂和二传手
}

  • 登录基地

public class LoginBase {private String token;私人用户;与吸气剂和二传手

}

  • 用户

public class User {private Integer userId;
私人String电子邮件;
私人整数状态;
私有字符串名称;

与吸气剂和二传手
}

  • 终于BaseResponse

public class BaseResponse {protected String statusCode;用吸气剂和二传手

My questions are, 1. Why do i get this error when i call the server

INFO:org.springframework.beans.factory.support.DefaultListableBeanFactory - 在org.springframework.beans.factory.support.DefaultListableBeanFactory@63de8f2d中预实例化单例:定义bean [restTemplate,JacksonObjectMapper];工厂层次结构的根目录WARN:org.springframework.web.client.RestTemplate - 对“https://abc.com/Rest/api/user/authenticate”的POST请求导致400(错误请求);调用错误处理程序线程“main”org.springframework.web.client.HttpClientErrorException中的异常:400 org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:90)的错误请求位于org.springframework.web.client . RestTemplate.handleResponseError(RestTemplate.java:494)at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:451)

2. how do i map json response to java LoginResponse

1 回答

  • 0

    您可能必须添加内容类型并接受请求的标头 .

    将响应映射到LoginResponse可以像这样直接完成

    LoginResponse lResponse = response.getBody();
    

    或者如果您正在使用restTemplate.postForObject(),则响应将采用LoginResponse的形式

相关问题