我使用Tomcat 7和Jersey和Moxy。我已經使用maven添加了jersey.server.jar,jersey.servlet.jar,jersey-core.jar,moxy(org.eclipse.persistence.moxy,版本2.5.0)。使用Jersey和Moxy的JAXB
我有接口(包名:org.kkm.test.model):
public interface Model extends BaseModel {
public String getConsumerId();
public void setConsumerId(String consumerId);
}
@XmlRootElement(name="reading")
@XmlType(propOrder={"consumerId", "address", "reading", "readingDate", "rotation", "readingStatus"})
public interface Reading extends Model {
@XmlElement
public Address getAddress();
@XmlElement
public double getReading();
@XmlElement
public Date getReadingDate();
@XmlElement
public short getRotation();
@XmlElement
public ReadingStatus getReadingStatus();
public void setAddress(Address address);
public void setReading(double reading);
public void setReadingDate(Date readingDate);
public void setRotation(short rotation);
public void setReadingStatus(ReadingStatus readingStatus);
}
public interface Address {
public String getAddress1();
public String getAddress2();
public String getAddress3();
public String getAddress4();
public void setAddress1(String address1);
public void setAddress2(String address2);
public void setAddress3(String address3);
public void setAddress4(String address4);
}
我有實現ReadingImpl和AddressImpl(包名稱:org.kkm.test.model.impl)。我有一個服務類,如下所示:
@Path("/sample")
public class TestService {
@GET
@Produces(MediaType.APPLICATION_XML)
@Path("/readings")
public List<Reading> getReadings() {
Reading reading1 = DependencySupplier.getReading();
Address address = DependencySupplier.getAddress();
address.setAddress1("Moolekkadavil");
address.setAddress2("Kottuvallikad");
address.setAddress3("Moothakunnam.P.O");
address.setAddress4("683516");
reading1.setAddress(address);
reading1.setConsumerId("1234567890");
reading1.setReading(11.00);
reading1.setReadingDate(new Date());
reading1.setReadingStatus(ReadingStatus.BILLED);
reading1.setRotation((short) 1);
Reading reading2 = DependencySupplier.getReading();
reading2.setAddress(address);
reading2.setConsumerId("1234567890");
reading2.setReading(11.00);
reading2.setReadingDate(new Date());
reading2.setReadingStatus(ReadingStatus.BILLED);
reading2.setRotation((short) 1);
List<Reading> list = new ArrayList<Reading>();
list.add(reading1);
list.add(reading2);
return list;
}
@GET
@Produces(MediaType.APPLICATION_XML)
@Path("/reading")
public Reading getReading() {
Reading reading1 = DependencySupplier.getReading();
Address address = DependencySupplier.getAddress();
address.setAddress1("Moolekkadavil");
address.setAddress2("Kottuvallikad");
address.setAddress3("Moothakunnam.P.O");
address.setAddress4("683516");
reading1.setAddress(address);
reading1.setConsumerId("1234567890");
reading1.setReading(11.00);
reading1.setReadingDate(new Date());
reading1.setReadingStatus(ReadingStatus.BILLED);
reading1.setRotation((short) 1);
return reading1;
}
}
我已經把一個jaxb.properties文件org.kkm.test.model有以下內容,以便MOXY可用於:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
我web.xml文件是:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Test JAX</display-name>
<servlet>
<servlet-name>testjax</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>org.kwa.test.service</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>testjax</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
我現在面臨的問題是: 當我調用「/ REST /樣品/讀數」我得到的XML正確的代表性,但是當我調用「/ REST /樣本/閱讀「我正在變得
com.sun.jersey.api.MessageException:Java消息正文編寫器 class ... etc錯誤。
然後我寫了一個MessageBodyWriter如下:
public class MyMessageBodyWriter implements MessageBodyWriter {
private List<Class<?>> types;
public MyMessageBodyWriter() {
types = new ArrayList<Class<?>>();
types.add(ReadingImpl.class);
}
public long getSize(Object obj, Class type, Type genericType, Annotation[] annotations,
MediaType mediaType) {
// TODO Auto-generated method stub
return -1;
}
public boolean isWriteable(Class type, Type genericType, Annotation[] annotations,
MediaType mediaType) {
return this.types.contains(type);
}
public void writeTo(Object target, Class type, Type genericType, Annotation[] annotations,
MediaType mediaType, MultivaluedMap headers, OutputStream outputStream)
throws IOException, WebApplicationException {
try {
JAXBContext ctx = JAXBContext.newInstance(type);
Marshaller marshaller = ctx.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(target, outputStream);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
}
現在,當我調用 「/ REST /樣品/寫有」 我正在以下XML:
<?xml version="1.0" encoding="UTF-8"?>
<address>
<address1>Moolekkadavil</address1>
<address2>Kottuvallikad</address2>
<address3>Moothakunnam.P.O</address3>
<address4>683516</address4>
</address><consumerId>1234567890</consumerId><reading>11.0</reading><readingDate>2013-07-04T21:16:39.128</readingDate><readingStatus>BILLED</readingStatus><rotation>1</rotation>
,你可以看到沒有根元素。因此,將「地址」元素視爲「額外內容」並將其顯示爲錯誤後,它將以「地址」作爲根元素和內容。