I've been developing Spring integration module for Foreman provisioning service
which exposes Rest interface:
http://theforeman.org/projects/foreman/wiki/API
For that purpose I use Spring RestTemplate with JSON (MappingJacksonHttpMessageConverter).
Request to Foreman:
https://192.168.199.44/environments?format=json
Response:
[{"environment":{"id":1,"name":"production"}}]
During Java call the following exception occurs:
Exception:Code:List<MediaType> mediaTypes = new ArrayList<MediaType>(); mediaTypes.add(MediaType.APPLICATION_JSON); HttpHeaders headers = new HttpHeaders(); headers.setAccept(mediaTypes); HttpEntity<Environment> httpEntity = new HttpEntity<Environment>(null, headers); try { ResponseEntity<Environment[]> responseEntity = restManager.exchange(foremanServiceUrl + "environments", HttpMethod.GET, httpEntity, Environment[].class); Environment[] envs = responseEntity.getBody(); return Arrays.asList( envs); } catch (RestClientException exception) { exception.printStackTrace(); throw exception; }
The Environment is annotated as follows:Code:Caused by: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "environment" (Class org.atsi.provisioning.foreman.service.model.environments.Environment), not marked as ignorable at [Source: org.apache.http.conn.EofSensorInputStream@12948069; line: 1, column: 18] (through reference chain: org.atsi.provisioning.foreman.service.model.envi ....
In Java client, the ObjectMapper is configured to support JAXB annotations:Code:@XmlRootElement(name="environment") public class Environment { private String id; private String name; public Environment() { } public Environment(String id, String name) { super(); this.id = id; this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString() { return new StringBuilder().append("[id=").append(id).append(",name=").append(name).append("]").toString(); } }
and the exception still occurs.Code:MappingJacksonHttpMessageConverter converter = applicationContext.getBean( "marshallingJacksonHttpMessageConverter", MappingJacksonHttpMessageConverter.class); ObjectMapper mapper = new ObjectMapper(); AnnotationIntrospector intr = new AnnotationIntrospector.Pair( new JaxbAnnotationIntrospector(), new JacksonAnnotationIntrospector() ); mapper.getDeserializationConfig().withAnnotationIntrospector(intr); mapper.getSerializationConfig().withAnnotationIntrospector(intr);
I read that I can set mapper.configure(DeserializationConfig.Feature.FAI L_ON_UNKNOWN_PROPERTIES, false),
but then the environment POJO is returned with null attributes.
I know there is possibilty to specify the auto-detect behaviour for JSON but I'm curious if this makes sense:
Code:mapper.setVisibility(JsonMethod.FIELD, JsonAutoDetect.Visibility.ANY) // auto-detect all member fields .setVisibility(JsonMethod.GETTER, JsonAutoDetect.Visibility.NONE) // but only public getters .setVisibility(JsonMethod.IS_GETTER, JsonAutoDetect.Visibility.NONE) // and none of "is-setters" ;


Reply With Quote
