Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-13530

Explain when and why use Jaxb2RootElementHttpMessageConverter vs MarshallingHttpMessageConverter

    Details

    • Type: Task
    • Status: Closed
    • Priority: Trivial
    • Resolution: Complete
    • Affects Version/s: 4.2 GA, 4.2.1
    • Fix Version/s: 4.2.3
    • Component/s: [Documentation], OXM
    • Labels:
    • Last commented by a User:
      false

      Description

      Hello

      I have the following configuration:

      	@Bean
          public MarshallingHttpMessageConverter marshallingMessageConverter() {
              MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter();
              converter.setMarshaller(jaxbMarshaller());
              converter.setUnmarshaller(jaxbMarshaller());
              return converter;
          }
      	
          @Bean
          public Jaxb2Marshaller jaxbMarshaller() {
              Jaxb2Marshaller jaxbMarshaller = new Jaxb2Marshaller();
              jaxbMarshaller.setClassesToBeBound(PersonaXml.class, PersonaCollection.class, GenericCollection.class);
                 
              Map<String, Object> props = new HashMap<>();
              props.put(Marshaller.JAXB_ENCODING,"UTF-8");
              props.put(Marshaller.JAXB_FORMATTED_OUTPUT, true);
              jaxbMarshaller.setMarshallerProperties(props);
              
              return jaxbMarshaller;
          }
      

      It works fine.

      Just in case consider that code works around with

      @XmlRootElement(name="persona")
      @XmlType(propOrder = {"id","nombre","apellido","fecha"})
      public class PersonaXml implements Serializable {
       
      	private static final long serialVersionUID = -2236860541513419944L;
      	
      	private String id;
      	private String nombre;
      	private String apellido;
      	
      	private Date fecha;
       
      	public PersonaXml(){
       
      	}
       
      	@XmlElement(name="id")
      	public String getId() {
      		return id;
      	}
      	public void setId(String id) {
      		this.id = id;
      	}
      	
      	@XmlElement(name="nombre")
      	public String getNombre() {
      		return nombre;
      	}
      	public void setNombre(String nombre) {
      		this.nombre = nombre;
      	}
      ...
      

      and

      @XmlRootElement(name="generic-collection")
      public class GenericCollection<T> {
       
      	private Collection<T> collection;
      	
      	public GenericCollection(){
      		
      	}
      	
      	public GenericCollection(Collection<T> collection){
      		this.collection = collection;
      	}
      	
      	@XmlElement(name="item")
          public Collection<T> getCollection(){
          	return collection;
          }
      }
      

      Again all work fine.

      Note: I am using JAXB2 annotations how you can see.

      It from package:

      • javax.xml.bind.annotation

      After to read some documentation I arrived to here
      MvcConfiguration.java

      Where it shows:

      @Override
      	public void configureMessageConverters(
      			List<HttpMessageConverter<?>> converters) {
       
      		// List is initially empty. Create and configure what we need.
      		MappingJacksonHttpMessageConverter jmc = new MappingJacksonHttpMessageConverter();
      		jmc.setPrettyPrint(true);
      		logger.info("Creating Jackson V1 convertor: "
      				+ jmc.getClass().getSimpleName());
      		converters.add(jmc);
       
      		Jaxb2RootElementHttpMessageConverter j2 = new Jaxb2RootElementHttpMessageConverter();
      		converters.add(j2);
      		return;
      	}
      

      I can see Paul uses Jaxb2RootElementHttpMessageConverter

      My question is how the titles says:

      • Explain when and why use Jaxb2RootElementHttpMessageConverter vs MarshallingHttpMessageConverter

      It for the reference documentation and for the API too

      Because after to check the current API, I can see in left part where all the classes/interfaces are listed the following too:

      1. Jaxb2CollectionHttpMessageConverter
      2. Jaxb2Marshaller
      3. Jaxb2RootElementHttpMessageConverter

      Where I already have used the second, but wondered when use the first and third.

      I am confused because my code already uses JABX2 annotations and through
      MarshallingHttpMessageConverter + Jaxb2Marshaller I can work in peace.

      So again, when use the first and third.

      Even more, in this other JIRA/post:
      Jaxb2RootElementHttpMessageConverter doesn't work well with class with @XmlAnyElement

      Rossen Stoyanchev says: "Have you considered using MarshallingHttpMessageConverter? It seems to match the behavior you want."

      Note: BTW I don't have explicitly declared JAXB2 how a dependency on my build.gradle file. Just Jackson 2. And all work fine.

        Activity

        Hide
        rstoya05-aop Rossen Stoyanchev added a comment -

        Generally speaking they have the same purpose. However the Jaxb2RootElementHttpMessageConverter does not require a dependency on spring-oxm. If however you're already using spring-oxm than MarshallingHttpMessageConverter is a good idea since it provides more ways to configure the Marshaller/Unmarshaller.

        For 4.3 we may make some improvements to Jaxb2RootElementHttpMessageConverter as part of SPR-10262.

        Show
        rstoya05-aop Rossen Stoyanchev added a comment - Generally speaking they have the same purpose. However the Jaxb2RootElementHttpMessageConverter does not require a dependency on spring-oxm. If however you're already using spring-oxm than MarshallingHttpMessageConverter is a good idea since it provides more ways to configure the Marshaller/Unmarshaller. For 4.3 we may make some improvements to Jaxb2RootElementHttpMessageConverter as part of SPR-10262 .
        Hide
        dr_pompeii Manuel Jordan added a comment -

        Thanks

        If however you're already using spring-oxm than MarshallingHttpMessageConverter is a good idea since it provides more ways to configure the Marshaller/Unmarshaller.

        Pls, could you expand the idea? I don't understand

        For 4.3 we may make some improvements to Jaxb2RootElementHttpMessageConverter as part of SPR-10262.

        Seems MarshallingHttpMessageConverter is better than Jaxb2RootElementHttpMessageConverter. Even more when that generic collection is mandatory to be created in XML when the user request a collection of entities

        Until now what I understood is that MarshallingHttpMessageConverter is more flexible or offers more settings.

        Am I correct?

        Show
        dr_pompeii Manuel Jordan added a comment - Thanks If however you're already using spring-oxm than MarshallingHttpMessageConverter is a good idea since it provides more ways to configure the Marshaller/Unmarshaller. Pls, could you expand the idea? I don't understand For 4.3 we may make some improvements to Jaxb2RootElementHttpMessageConverter as part of SPR-10262 . Seems MarshallingHttpMessageConverter is better than Jaxb2RootElementHttpMessageConverter. Even more when that generic collection is mandatory to be created in XML when the user request a collection of entities Until now what I understood is that MarshallingHttpMessageConverter is more flexible or offers more settings. Am I correct?
        Hide
        rstoya05-aop Rossen Stoyanchev added a comment - - edited

        Jaxb2RootElementHMC is a simpler alternative to MarshallingHMC which in turn requires spring-oxm as as dependency but offers more features. Can you clarify what is unclear?

        Show
        rstoya05-aop Rossen Stoyanchev added a comment - - edited Jaxb2RootElementHMC is a simpler alternative to MarshallingHMC which in turn requires spring-oxm as as dependency but offers more features. Can you clarify what is unclear?
        Hide
        dr_pompeii Manuel Jordan added a comment -

        Hi

        Jaxb2RootElementHMC is a simpler alternative to MarshallingHMC which in turn requires spring-oxm as as dependency but offers more features

        Consider include that explanation in the Reference documentation

        Can you clarify what is unclear?

        Was, your english context for

        If however you're already using spring-oxm than MarshallingHttpMessageConverter is a good idea since it provides more ways to configure the Marshaller/Unmarshaller.

        English is not my native language and I was tired… sorry.

        Thanks

        Show
        dr_pompeii Manuel Jordan added a comment - Hi Jaxb2RootElementHMC is a simpler alternative to MarshallingHMC which in turn requires spring-oxm as as dependency but offers more features Consider include that explanation in the Reference documentation Can you clarify what is unclear? Was, your english context for If however you're already using spring-oxm than MarshallingHttpMessageConverter is a good idea since it provides more ways to configure the Marshaller/Unmarshaller. English is not my native language and I was tired… sorry. Thanks

          People

          • Assignee:
            rstoya05-aop Rossen Stoyanchev
            Reporter:
            dr_pompeii Manuel Jordan
            Last updater:
            Stéphane Nicoll
          • Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:
              Days since last comment:
              2 years, 16 weeks, 1 day ago