Java JAX-RS/Jersey+;text/xml:我可以指定xml样式表吗?
我已经使用实现了一个REST服务。当响应的mime类型为“text/xml”时,是否有方法指定关联的mime类型 多谢各位 更新:以下是一段代码:Java JAX-RS/Jersey+;text/xml:我可以指定xml样式表吗?,java,xslt,jersey,jax-rs,Java,Xslt,Jersey,Jax Rs,我已经使用实现了一个REST服务。当响应的mime类型为“text/xml”时,是否有方法指定关联的mime类型 多谢各位 更新:以下是一段代码: @Path("/service") @Stateless public class MyServices { @PersistenceContext(unitName = "em") private EntityManager em; @Path("/id/{id}") @GET public MyC
@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;
}
}
使用OPs注释编辑: 我将使用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();
StringWriter编写器=新建StringWriter();
//添加带有转义引号的“手动”处理说明
//还是单个标记
writer.println(“”);
writer.println(“”);
//创建并配置封送拆收器以省去处理指令
Marshaller=context.createMarshaller();
setProperty(marshaller.JAXB_片段,Boolean.TRUE);
//编剧元帅
马歇尔(某物,作家);
//获取字符串表示形式
String str=writer.toString();
当然,您也可以直接打印到所需的其他任何输出流,例如文件或Sytstem.out
将
放在文档的其余部分之前?请参见:
有一个很好的注释可以很好地与JAXB配合使用
@Path("/id/{id}")
@GET
@Produces("application/xml")
@XmlHeader("<?xml-stylesheet type=\"text/css\" href=\"something.css\"?>")
public MyClass getById(@PathParam("id")long id)
@Path(“/id/{id}”)
@得到
@生成(“应用程序/xml”)
@XmlHeader(“”)
公共MyClass getById(@PathParam(“id”)长id)
向我们展示您是如何发布服务的(您用@GET
,@POST
注释的方法)etc@AnthonyAccioly我已经添加了代码。我的服务返回一个用@XmlRootElement标记的类的实例。我不想管理到XML的转换。
@Path("/id/{id}")
@GET
@Produces("application/xml")
@XmlHeader("<?xml-stylesheet type=\"text/css\" href=\"something.css\"?>")
public MyClass getById(@PathParam("id")long id)