再次寻求专家的帮助/指导,
我的问题是,我需要创建动态网站,它调用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);
}
}
这是我的服务器的春天配置,
<?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扩展了BaseResponse {
私有LoginBase数据;
与吸气剂和二传手
}
public class LoginBase { 私有字符串标记;
私人用户用户;with getters and setters }
公共类用户{
private Integer userId; private String email; private Integer status; private String name; with getters and setters }
公共类BaseResponse {
protected String statusCode;with getter and setter }
我的问题是, 1.当我呼叫服务器时,为什么会出现此错误
INFO:org.springframework.beans.factory.support.DefaultListableBeanFactory - 在org.springframework.beans.factory.support.DefaultListableBeanFactory@63de8f2d中预先实例化单例:定义bean [restTemplate,JacksonObjectMapper];工厂层级的根 警告:org.springframework.web.client.RestTemplate - 对“https://abc.com/Rest/api/user/authenticate”的POST请求导致400(错误请求);调用错误处理程序 线程“main”中的异常org.springframework.web.client.HttpClientErrorException:400 Bad Request at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:90) 在org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:494) 在org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:451)
2。我如何将json响应映射到java LoginResponse
答案 0 :(得分:0)
您可能需要添加内容类型并接受请求的标头。
将响应映射到LoginResponse可以像这样直接完成
LoginResponse lResponse = response.getBody();
或者如果您使用的是restTemplate.postForObject(),则响应将采用LoginResponse的形式