Skip to content

Instantly share code, notes, and snippets.

@mraible
Created September 29, 2014 13:11
Show Gist options
  • Save mraible/f409e6715ed44dd721c2 to your computer and use it in GitHub Desktop.
Save mraible/f409e6715ed44dd721c2 to your computer and use it in GitHub Desktop.
Returning JAXBElement
import org.ncpdp.schema.transport.MessageType;
import org.ncpdp.schema.transport.ObjectFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.xml.bind.JAXBElement;
@RestController
public class HelloWorldController {
@RequestMapping(value = "/api/message", method = RequestMethod.GET)
public JAXBElement<MessageType> messageType() {
MessageType type = new MessageType();
return new ObjectFactory().createMessage(type);
}
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.xml.MarshallingHttpMessageConverter;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import java.util.Arrays;
import java.util.List;
@Configuration
@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(marshallingHttpMessageConverter());
}
@Bean
public MarshallingHttpMessageConverter marshallingHttpMessageConverter() {
Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
jaxb2Marshaller.setPackagesToScan(new String[]{"com.ncpdb.schema.transport"});
MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter();
converter.setMarshaller(jaxb2Marshaller);
converter.setUnmarshaller(jaxb2Marshaller);
converter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_XML));
return converter;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment