首先需要引入eclipselink包:
org.eclipse.persistence
eclipselink
2.5.0
实体类包下面需要添加jaxb.properties文件:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
实体类使用如下注解:
package com.chinamobile.cmss.ehub.ssb.test.entity;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.oxm.annotations.XmlCDATA;
@XmlRootElement(name="InterBOSS")
@XmlAccessorType(XmlAccessType.FIELD)
public class UserStateRqeMessage {
@XmlCDATA
private String SvcCont;
public String getSvcCont() {
return SvcCont;
}
public void setSvcCont(String svcCont) {
SvcCont = svcCont;
}
/*private UserStateDetail SvcCont;
public UserStateDetail getSvcCont() {
return SvcCont;
}
public void setSvcCont(UserStateDetail svcCont) {
SvcCont = svcCont;
}*/
}
controller接口使用@ResponseBody和@RequestBody
package com.chinamobile.cmss.ehub.ssb.test.controller;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.chinamobile.cmss.ehub.ssb.test.entity.UserStateRqeMessage;
import com.chinamobile.cmss.ehub.ssb.test.entity.UserStateRspMessage;
@Controller
public class UserController {
@ResponseBody
@RequestMapping(value="/users", method=RequestMethod.POST)
public UserStateRspMessage checkUserState(@RequestBody UserStateRqeMessage userStateRqeMessage) throws JAXBException{
System.out.println(userStateRqeMessage.getSvcCont());
JAXBContext jc = JAXBContext.newInstance(UserStateRspMessage.class);
UserStateRspMessage userStateRspMessage = new UserStateRspMessage();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(userStateRspMessage, System.out);
return userStateRspMessage;
}
}