在Jersey响应中设置XML标头

时间:2014-06-04 14:53:02

标签: java web-services jaxb jersey

我有几个通过Jersey / Jaxb实现的Web服务,它们正在发送XML响应

<?xml version="1.0" encoding="UTF-8"?> 

我想编辑此标头并包含standalone="yes"。我知道我可以使用marshaller.setProperty(..)方法来解决这个问题,但是更改从项目发送的每个响应的响应代码将会有很多工作。

有没有更好的方法在项目中实现这一目标?设置一些自定义Jaxb属性文件或某些配置或什么?


修改

所以我设法实现了我想要的但我不确定这是否是正确的方法。我写了一个自定义marshaller解析器并覆盖xml标头。

@Provider
public class JaxbMarshallerResolver implements ContextResolver<Marshaller> 
{

    @Override
    public Marshaller getContext(Class<?> type) 
    {
        Marshaller marshaller;
        try
        {
            JAXBContext jc = JAXBContext.newInstance(type);
            marshaller = jc.createMarshaller();
            marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
            marshaller.setProperty("com.sun.xml.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
        }
        catch (JAXBException je)
        {
            throw new RuntimeException(je);
        }
        return marshaller;
    }
}

有没有更好的方法来实现它?

0 个答案:

没有答案
相关问题