有没有一种方法可以检索XmlAdapter中的当前基本URI?或通常如何存档?
public class Service{
...
@GET
public MyEntity getEntity() {
return em.find(MyEntity.class, "dummy");
}
...
}
@XmlRootElement(name = "myEntity")
public class MyEntity {
@XmlJavaTypeAdapter(MyAdapter.class)
private Entity2 entity2Ref;
...
}
public class MyAdapter extends XmlAdapter<Entity2Ref, Entity2> {
// Is NULL but shold be injected with host URI
@Context
UriInfo uri;
...
}
最佳答案
以下是如何完成此操作的完整示例:
XML回应
下面,我将演示如何通过知道address
的XmlAdapter
将UriInfo
元素中的URI放入以下响应。
<?xml version="1.0" encoding="UTF-8"?>
<customer id="1">
<name>Jane Doe</name>
<address>http://localhost:9999/address/123</address>
</customer>
Java模型
下面是我将用于此示例的Java模型。
顾客
默认情况下,
Address
类的内容将编组在customer
元素下面。我们将使用XmlAdapter
对此执行特殊处理。import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlRootElement
@XmlType(propOrder={"name", "address"})
public class Customer {
private int id;
private String name;
private Address address;
@XmlAttribute
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlJavaTypeAdapter(AddressAdapter.class)
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
地址
import javax.xml.bind.annotation.XmlAttribute;
public class Address {
private int id;
@XmlAttribute
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
XmlAdapter
以下是我们将使用的
XmlAdapter
。注意,它如何从AddressResource
获取信息以构建URI
。它需要一个UriInfo
,这使其具有状态。我们将需要在XmlAdapter
上设置此Marshaller
的实例,以使一切正常工作。import javax.ws.rs.core.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class AddressAdapter extends XmlAdapter<String, Address> {
private UriInfo uriInfo;
public AddressAdapter() {
}
public AddressAdapter(UriInfo uriInfo) {
this.uriInfo = uriInfo;
}
@Override
public Address unmarshal(String v) throws Exception {
// TODO Auto-generated method stub
return null;
}
@Override
public String marshal(Address v) throws Exception {
if(null == uriInfo) {
return "";
}
UriBuilder builder = UriBuilder.fromResource(AddressResource.class);
System.out.println(uriInfo.getAbsolutePath().getHost());
builder.scheme(uriInfo.getAbsolutePath().getScheme());
builder.host(uriInfo.getAbsolutePath().getHost());
builder.port(uriInfo.getAbsolutePath().getPort());
builder.path(AddressResource.class, "get");
return builder.build(v.getId()).toString();
}
}
JAX-RS服务
在此示例中,有两个服务,一个用于
Address
,另一个用于Customer
。地址资源
import javax.ws.rs.*;
import javax.ws.rs.ext.Provider;
@Provider
@Path("/address")
public class AddressResource {
@GET
@Path("{id}")
public Address get(@PathParam("id") int id) {
Address address = new Address();
address.setId(id);
return address;
}
}
客户资源
由于我们有状态
XmlAdapter
,因此我们不能仅通过默认绑定来利用JAXB。相反,我们可以通过StreamingOutput
访问JAXB。import javax.ws.rs.*;
import javax.ws.rs.core.*;
import javax.ws.rs.ext.Provider;
import javax.xml.bind.*;
@Provider
@Path("/customer")
public class CustomerResource {
private JAXBContext jaxbContext;
public CustomerResource() {
try {
jaxbContext = JAXBContext.newInstance(Customer.class);
} catch (JAXBException e) {
// TODO - Handle Exception
}
}
@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_XML)
public StreamingOutput get(@Context UriInfo uriInfo, @PathParam("id") int id) {
Customer customer = new Customer();
customer.setId(id);
customer.setName("Jane Doe");
Address address = new Address();
address.setId(123);
customer.setAddress(address);
return new MyStreamingOutput(jaxbContext, customer, uriInfo);
}
}
流输出
import java.io.*;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.*;
import javax.xml.bind.*;
public class MyStreamingOutput implements StreamingOutput {
private JAXBContext jaxbContext;
private Object object;
private UriInfo uriInfo;
public MyStreamingOutput(JAXBContext jc, Object object, UriInfo uriInfo) {
this.jaxbContext = jc;
this.object = object;
this.uriInfo = uriInfo;
}
@Override
public void write(OutputStream os) throws IOException,
WebApplicationException {
try {
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setAdapter(new AddressAdapter(uriInfo));
marshaller.marshal(object, os);
} catch(JAXBException jaxbException) {
throw new WebApplicationException(jaxbException);
}
}
}
关于java - 在XmlAdapter中获取当前主机,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17969343/