gpt4 book ai didi

java - 在 XmlAdapter 中获取当前主机

转载 作者:行者123 更新时间:2023-12-02 06:51:30 25 4
gpt4 key购买 nike

有没有办法在 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 元素中的 URI 通过识别 XmlAdapter 放入UriInfo.

<?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;
}

}

Xml适配器

下面是我们将使用的XmlAdapter。请注意它如何从 AddressResource 获取信息以构建 URI。它需要一个 UriInfo,这使其成为有状态的。我们需要在 Marshaller 上设置此 XmlAdapter 的实例,以使一切正常工作。

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,因此我们不能仅通过默认绑定(bind)来利用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com