JAXB classes: reuse for REST

2019-07-11 19:00发布

Tech Stack: Java 1.6, JPA (Hibernate 3), Spring 3, Oracle 11g

Hello,

We are currently refactoring our code to move away from SOAP based web-services to REST. I've got loads of XSD, these were used for SOAP based WS. And there are tons of JAXB classes generated using these schema files.

My question is can I use the same JAXB classes for REST (in and out) or not. I've no experience with REST.

1条回答
Animai°情兽
2楼-- · 2019-07-11 19:33

It's better than you might think. Just take your JAXB object that you used previously for SOAP endpoints and return them from controllers:

public @ResponseBody JAXBElement<JaxbResponseType> 
controllerMethod(@RequestPayload JAXBElement<JaxbRequestType> request) {
    //...
}

Spring, seeing JAXB annotations, will automatically marshall them to XML and JSON. This should be a good starting point, not to mention your REST services will have similar structure to SOAP.

查看更多
登录 后发表回答