Возврат сгенерированных JAXB элементов из Spring Boot Controller
Я генерирую множество файлов Java изhttp://www.ncpdp.orgXSD файлы (доступны только для участников). После их создания я хотел бы использовать их в своих Spring Controllers, но у меня возникают проблемы с преобразованием ответов в XML.
Я попытался вернуть сам элемент, а также JAXBElement <T>, но ни один из них не работает. Тест ниже не проходит:
java.lang.AssertionError: Status
Expected :200
Actual :406
@Test
public void testHelloWorld() throws Exception {
mockMvc.perform(get("/api/message")
.accept(MediaType.APPLICATION_XML)
.contentType(MediaType.APPLICATION_XML))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_XML));
}
Вот мой контроллер:
import org.ncpdp.schema.transport.MessageType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@RequestMapping(value = "/api/message", method = RequestMethod.GET)
public MessageType messageType() {
return new MessageType();
}
}
Я пытался создать MvcConfig для переопределения конфигурации MVC Spring Boot, но, похоже, он не работает.
@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;
}
}
Что мне нужно сделать, чтобы заставить Spring MVC маршалировать мои сгенерированные объекты JAXB в XML?