1

我已经使用jersey实现了 REST 服务。当响应的 mime 类型为“text/xml”时,有没有办法指定关联的xml-stylesheet

谢谢你。

更新:这是一段代码:

@Path("/service")
@Stateless
public class MyServices
    {
    @PersistenceContext(unitName = "em")
    private EntityManager em;

    @Path("/id/{id}")
    @GET
    public MyClass getById(@PathParam("id")long id)
        {
        MyClass o=em.find(MyClass.class, id);
        return o;
        }
     }

.

@Entity(name="X")
@XmlRootElement(name="X")
@NamedQueries(...)
public class MyClass
    implements Serializable
    {
    private static final long serialVersionUID = 1L;

    ...
        }
4

2 回答 2

2

请参阅:有没有办法修改 Jersey 生成的 XML 响应以包含样式表信息?

有一个很好的XmlHeader注释可以很好地与 JAXB 配合使用。

@Path("/id/{id}")
@GET
@Produces("application/xml")
@XmlHeader("<?xml-stylesheet type=\"text/css\" href=\"something.css\"?>")
public MyClass getById(@PathParam("id")long id)
于 2013-02-06T21:56:21.740 回答
0

用 OP 评论编辑:

我会使用 StringWriter 首先将样式表信息写入其中,然后将对象编组到其中:

StringWriter writer = new StringWriter();
//add processing instructions "by hand" with escaped quotation marks
//or single marks
writer.println("<?xml version='1.0'?>");
writer.println("<?xml-stylesheet type=\"text/xsl\" href=\"\">");

//create and configure marshaller to leave out processing instructions
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

//marshal to the StringWriter
marshaller.marshal(someObject,writer);
//get the string representation 
String str = writer.toString();

当然,您也可以直接打印到您想要的每个其他输出流,例如文件或 Sytstem.out。

放在<?xml-stylesheet href="my-style.css"?>文档的其余部分之前?

于 2013-02-06T21:43:42.907 回答