springmvc5restapi使用jaxb将xml转换为bean

1l5u6lss  于 2021-07-22  发布在  Java
关注(0)|答案(0)|浏览(210)

我们正在将jersey端点迁移到spring mvc rest端点。这些端点使用并生成xml。但是springmvc无法转换xml输入bean,因为xml标记和bean的属性名不同。我必须将请求作为字符串,并使用jaxb显式地转换它。有没有更好的办法?
web配置

@EnableWebMvc
@Configuration
public class WebConfig implements WebMvcConfigurer {

        public static final String JAXB_PACKAGE = "com.comp.platform.dataholder";

        @Autowired
        private Jackson2ObjectMapperBuilder builder;

        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

            converters.add(new StringHttpMessageConverter());
            converters.add(new FormHttpMessageConverter());
            converters.add(createXmlHttpMessageConverter());

            converters.add(createMappingJacksonHttpMessageConverter(builder));
            // converters.add(createMappingJackson2XmlHttpMessageConverter(xmlMapper));

            Jaxb2RootElementHttpMessageConverter jaxb2RootElementHttpMessageConverter = new Jaxb2RootElementHttpMessageConverter();
            jaxb2RootElementHttpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_XML));
            converters.add(jaxb2RootElementHttpMessageConverter);

        }

        @Bean
        public Jackson2ObjectMapperBuilder customJson() {
            return new Jackson2ObjectMapperBuilder().indentOutput(true).serializationInclusion(JsonInclude.Include.NON_NULL)
                    .propertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE).failOnUnknownProperties(false);
        }

        @Bean
        @Primary
        public ObjectMapper createObjectMapper(Jackson2ObjectMapperBuilder builder) {

            ObjectMapper mapper = builder.build();
            mapper.setSerializationInclusion(Include.NON_NULL);
            return mapper;
        }

        @Bean
        public Jaxb2Marshaller jaxb2Marshaller() {
            Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
            marshaller.setClassesToBeBound(NonFinancialDataHolder.class, JaxbDataHolder.class, ViestimateDataHolder.class,CTRequest.class,com.app.rest.PolicyDetailRequest.class,com.app.rest.TransactionSubmitRequest.class,com.app.rest.AgentSuppressInfoRequest.class);

            return marshaller;
        }

        /**
         * @return
         */
        @Bean
        public MappingJackson2HttpMessageConverter createMappingJacksonHttpMessageConverter(
                Jackson2ObjectMapperBuilder builder) {
            MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
            converter.setObjectMapper(builder.build());
            return converter;
        }

        @Autowired
        Jaxb2Marshaller jaxb2Marshaller;

        private HttpMessageConverter<Object> createXmlHttpMessageConverter() {
            Assert.notNull(jaxb2Marshaller, "Jaxb Marshaller dependency required");
            MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter(jaxb2Marshaller);
            xmlConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_XML));

            return xmlConverter;
        }

        public JAXBContext getMarshaller() throws JAXBException {
            return JAXBContext.newInstance(JAXB_PACKAGE);
        }

    }

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
        "_UserId",
        "_Password",
        "_RequestDateTime",
        "_Policy"
})
    @XmlRootElement(name = "PolicyDetailRequest")
    public class PolicyDetailRequest
    {
        @XmlElement(name = "UserId")
        protected String _UserId;
        @XmlElement(name = "Password")
        protected String _Password;
        @XmlElement(name = "RequestDateTime")
        protected String _RequestDateTime;
        @XmlElement(name = "Policy")
        protected String _Policy;
    }

静止端点

@Slf4j 
@RestController    
@RequestMapping("/api")  
public class api
{
    @Autowired
        Jaxb2Marshaller jaxb2Marshaller;

    @PostMapping(value="/PolicyResponse",produces=MediaType.APPLICATION_XML_VALUE,consumes = MediaType.APPLICATION_XML_VALUE)
    @ResponseBody
    public PolicyDetailResponse getPolicyResponse(@RequestBody String data)
    {
        PolicyDetailRequest request = (PolicyDetailRequest) jaxb2Marshaller.unmarshal(new StreamSource(new StringReader(data)));
        PolicyDetailResponse response = new PolicyDetailResponse();
        InternetRSHandler handler = new InternetRSHandler();

        response= handler.getPolicyRSResponse(request);

        return response;

    }
}

输入请求

<PolicyDetailRequest>
    <UserId>1234567890</UserId> 
    <Password>password</Password> 
    <RequestDateTime>20200420-15:07:23</RequestDateTime>
    <Policy>A76ABCD12</Policy>
</PolicyDetailRequest>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题