Created
September 29, 2014 13:11
-
-
Save mraible/f409e6715ed44dd721c2 to your computer and use it in GitHub Desktop.
Returning JAXBElement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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