http://www.ncpdp.org의 XSD 파일 (구성원 만 사용 가능)에서 많은 Java 파일을 생성 중입니다. 생성 후, Spring Controller에서 사용하고 싶지만 응답을 XML로 변환하는 데 문제가 있습니다.스프링 부트 컨트롤러에서 JAXB 생성 요소를 반환
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));
}
여기 내 컨트롤러 : 나는 봄 부팅의 MVC의 설정을 무시하는 MvcConfig을 만드는 시도했습니다
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();
}
}
했지만, 제대로 동작하지 않습니다.
@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;
}
}
생성 된 JAXB 객체를 XML로 마샬링하기 위해 Spring MVC를 얻으려면 어떻게해야합니까?
질문 : 생성 된'MessageType'은 그것에'@XmlRootElement' 주석을 가지고 있습니까? 기본 XML 변환기 인'Jaxb2RootElementHttpMessageConverter'가이 주석을 사용하여 마샬링을 지원할 MessageType의 인스턴스를 고려해야합니다. –
@BijuKunjummen이 맞습니다! https://github.com/spring-projects/spring-boot/issues/407 – geoand
GET에 실제로 content-type application/xml이 있습니까? 어쩌면 변환 할 수없는 요청일까요? –