使用Spring RestTemplate发布JSON

时间:2013-11-21 23:07:31

标签: json spring resttemplate

我已经将Java对象转换为json String,并且我尝试使用RestTemplate将其发布到其他Web服务,并且我总是得到500错误。以下是详细信息:

User createJSONUser = createUser(); // provides me the java object

//converting the javaobject to json string successfully using jackson in the next two lines     
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writeValueAsString(createJSONUser);

//creating the headers 
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.APPLICATION_JSON);
requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json")));

//setting the message converter
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());

//posting the json using resttemplate
ResponseEntity<String> response = restTemplate.exchange(sandBoxURL, HttpMethod.POST, new HttpEntity<String>(jsonString, requestHeaders), String.class);

我正在获取resttemplate作为bean,我在context-xml中定义如下:

    <?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:util="http://www.springframework.org/schema/util"
      xsi:schemaLocation="
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">

        <bean id="responseMessageConverter" class = "com.uhg.iam.esso.rest.client.converters.CustomResponseConverter">
            <property name="supportedMediaTypes" value="application/xml"/>
        </bean>
        <bean id="customErrorHandlerForEssoRestServer" class="com.uhg.iam.esso.rest.client.EssoRestClientErrorHandler"/> 
        <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"/>
        <bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
            <property name="supportJaxbElementClass" value="true"/>
            <property name="classesToBeBound">
                <list>
                    <value>esso.schemas.core._1.Response</value>
                </list>
            </property>
        </bean>    

        <bean id="marshallingHttpMessageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
            <property name="marshaller" ref="jaxb2Marshaller"/>
            <property name="unmarshaller" ref="jaxb2Marshaller"/> 
        </bean>

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

        <bean id="restTemplateBean" class="org.springframework.web.client.RestTemplate">
            <property name="errorHandler" ref="customErrorHandlerForEssoRestServer"/>
            <property name="messageConverters">
                <list>
                    <!-- <ref bean="marshallingHttpMessageConverter"/> 
                    <ref bean="responseMessageConverter"/> -->
                    <ref bean="jsonHttpMessageConverter"/> 
                    <ref bean="stringHttpMessageConverter"/> 
                </list>
            </property>
        </bean>
        <bean id="essoRestClientDelegate" class="com.uhg.iam.esso.rest.client.EssoRestClientJSONDelegate">
            <property name="restTemplate" ref="restTemplateBean"/>
        </bean>

    </beans>

Can someone please point out where I am making a mistake?

0 个答案:

没有答案