Announcement Announcement Module
Collapse
No announcement yet.
Spring Rest to Foreman - unknown JSON field Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Rest to Foreman - unknown JSON field

    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:
    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;
    		}
    Exception:
    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 ....
    The Environment is annotated as follows:
    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();
    	}
    }
    In Java client, the ObjectMapper is configured to support JAXB annotations:

    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);
    and the exception still occurs.
    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"
    	        ;

  • #2
    Originally posted by MarcinJarzab View Post
    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:
    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;
    		}
    Exception:
    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 ....
    The Environment is annotated as follows:
    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();
    	}
    }
    In Java client, the ObjectMapper is configured to support JAXB annotations:

    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);
    and the exception still occurs.
    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"
    	        ;
    You should add one more class with a single field
    Code:
    public class EnvironmentWrapper {
    
    	private Environment environment;
    
    	public Environment getEnvironment() {
    		return environment;
    	}
    
    	public void setEnvironment(Environment environment) {
    		this.environment = environment;
    	}
    
    }
    
    
    		    ResponseEntity<List<EnvironmentWrapper>> responseEntity = restManager.exchange(foremanServiceUrl + "environments", HttpMethod.GET, httpEntity, List<?>.class);
    
    
    The exception basically means it does not know what the "environment" should be mapped too.

    Comment

    Working...
    X