2012-09-06 128 views
0

再次的IAM尋找從專家的幫助/指南,RestTemplate POST使用JSON

我的問題是,我需要創建動態Web網站,其中調用REST類型的服務器,以獲得數據,所有的請求是POST和回報json對象。 Iam考慮使用Spring RestTemplate來調用服務器。我的服務器工作正常,這意味着目前一些android和蘋果應用程序連接到相同的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); 

    } 
} 

這是服務器的我的春天CONFIGS,

<?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" /> 

這是ho W I使用它(測試)

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", "[email protected]"); 
    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); 

我的登錄響應類,它的子類,

  1. LoginResponse

公共類LoginResponse擴展BaseResponse {
私人LoginBase數據; 與getter和setter
}

  1. 登錄基地

公共類LoginBase { 私人字符串令牌;
私人用戶用戶;

with getters and setters 

} 
  1. 用戶

公共類用戶{

private Integer userId; 
    private String email; 
    private Integer status;  
    private String name; 

     with getters and setters 
} 
  1. finall ŸBaseResponse

公共類BaseResponse {
保護字符串的StatusCode;

with getter and setter } 

我的問題是, 1.當我調用服務器

INFO爲什麼我得到這個錯誤:org.springframework.beans.factory.support.DefaultListableBeanFactory - 預在org.s[email protected]63de8f2d中定義單例:定義bean [restTemplate,JacksonObjectMapper]; WARNING:org.springframework.web.client.RestTemplate - POST請求「https:// abc。com/Rest/api/user/authenticate「導致400(錯誤請求);調用錯誤處理程序 線程」main「中的異常org.springframework.web.client.HttpClientErrorException:400錯誤的請求 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.如何映射到Java LoginResponse JSON響應

回答

0

您可能必須將內容類型和接受標頭添加到您的請求中。

映射到LoginResponse響應可以直接進行這樣

LoginResponse lResponse = response.getBody();  

或如果使用restTemplate.postForObject(),所述效應初探將在LoginResponse

形式