Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explain when and why use Jaxb2RootElementHttpMessageConverter vs MarshallingHttpMessageConverter [SPR-13530] #18107

Closed
spring-issuemaster opened this issue Oct 2, 2015 · 4 comments

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link
Collaborator

commented Oct 2, 2015

Manuel Jordan opened SPR-13530 and commented

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.


Affects: 4.2 GA, 4.2.1

Referenced from: commits da707c1

0 votes, 6 watchers

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Nov 6, 2015

Rossen Stoyanchev commented

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 #14895.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Nov 6, 2015

Manuel Jordan commented

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 #14895.

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?

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Nov 6, 2015

Rossen Stoyanchev commented

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?

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Nov 6, 2015

Manuel Jordan commented

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.