我再次寻求专家的帮助/指导,
我的问题是,我需要创建动态网站来调用 restfull 服务器来获取数据,所有请求都是 POST 并返回 json 对象。我正在考虑使用 Spring RestTemplate 来调用服务器。我的服务器工作正常,这意味着目前一些 android 和 Apple 应用程序连接到相同的 API 并且它们工作正常。但是当我尝试使用 RestTemplate 连接到服务器时,它给出了一些错误
org.springframework.web.client.HttpClientErrorException:400 错误请求
这是我的服务器,
@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 数据;使用 getter 和 setter
}
- 登录基地
公共类 LoginBase { 私有字符串令牌;
私人用户用户;with getters and setters }
- 用户
公共类用户{
private Integer userId; private String email; private Integer status; private String name; with getters and setters }
- 最后是 BaseResponse
公共类 BaseResponse {
受保护的字符串状态码;with getter and setter }
我的问题是,1.为什么我在调用服务器时会收到此错误
信息: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(错误请求);在 org.springframework.web.client 的 org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:90) 的线程“main”中调用错误处理程序异常 org.springframework.web.client.HttpClientErrorException: 400 Bad Request。 RestTemplate.handleResponseError(RestTemplate.java:494) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:451)
2.我如何将json响应映射到java LoginResponse