Note that there are some explanatory texts on larger screens.

plurals
  1. PORestTemplate unmarshal errorBody
    primarykey
    data
    text
    <p>Good day.</p> <p>I am writing REST client on Spring. The server can send in response an XML or JSON marshalled object. In case of normal request/responce its works fine. My RestTemplate client can unmarshall response. But in case of 404 error the server send an error descriptor as XML/JSON into response body.</p> <p>I don't know how to unmarshall error request body.</p> <p>This is my REST client code:</p> <pre class="lang-java prettyprint-override"><code>@Service public class XsdClientImpl implements InitializingBean, XsdClient { private static final Logger LOGGER = LoggerFactory.getLogger(XsdClientImpl.class); @Autowired @Qualifier("xsdClientRestTemplate") private RestTemplate restTemplate; private String baseUrl; @Override public XsdInfoType fetchNamespace(String namespace) throws XsdClientException { Map&lt;String, String&gt; vars = Collections.singletonMap("namespace", namespace); try { return restTemplate.getForObject(baseUrl + "/xsd/get/{namespace}.xml", XsdInfoType.class, vars); } catch (final HttpClientErrorException e) { // e.getResponseBodyAsByteArray() how to unmarshall it? it can be XML or JSON return null; } } } </code></pre> <p>This is fragment of Spring config:</p> <pre class="lang-xml prettyprint-override"><code>&lt;bean id="objectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean" p:indentOutput="true" p:simpleDateFormat="yyyy-MM-dd'T'HH:mm:ss.SSSZ"&gt; &lt;/bean&gt; &lt;bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" p:targetObject-ref="objectMapper" p:targetMethod="registerModule"&gt; &lt;property name="arguments"&gt; &lt;list&gt; &lt;bean class="com.fasterxml.jackson.datatype.joda.JodaModule"/&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; &lt;bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"/&gt; &lt;bean id="jaxb2RootElementHttpMessageConverter" class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/&gt; &lt;bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"&gt; &lt;property name="objectMapper" ref="objectMapper"/&gt; &lt;/bean&gt; &lt;bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:location="classpath:xsd-client.properties"/&gt; &lt;bean class="org.springframework.web.client.RestTemplate" id="xsdClientRestTemplate"&gt; &lt;property name="messageConverters"&gt; &lt;list&gt; &lt;ref bean="mappingJackson2HttpMessageConverter"/&gt; &lt;ref bean="jaxb2RootElementHttpMessageConverter"/&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; </code></pre> <p>Note: In normal case REST server send XsdInfoType marshalled object. But on error response object has other type.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload