2013-05-21 77 views
1

我有一个WS,它会生成一条消息SOAP 消息。由于尺寸限制,我想删除不必要的空格(用于缩进)和新行。如何在使用生成的类和注释时执行此操作(@WebService@WebMethod)?在我所看到的例子是这样完成的:从jaxb xml响应中删除空格

Marshaller m = jc.createMarshaller(); 
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE); 

不过,我不是手动创建Marshaller,所以我不知道我在哪里可以添加此属性,如果是这样做的正确方法。 JAXB的实现是axis2

回答

1

创建自定义的JAXBContext和下面提到注释您的web服务:

@WebService(serviceName = "Hello") 
    @UsesJAXBContext(value = CustomJaxbContext.class) 
    public class HelloWS 
    { ... 
    } 

    public class HelloJaxbContext implements JAXBContextFactory 
    { 
     @Override 
     public JAXBRIContext createJAXBContext(SEIModel seim, List<Class> classesToBind, List<TypeReference> typeReferences) throws JAXBException { 
      //JAXBRIContext extends JAXBContext, so you should be able to set the desired marshaller properties 
      //create your jaxb context with necessary properties for marshalling 
      return yourJAXBRIContext; 
     } 
    } 

参考http://javasourcecode.org/html/open-source/jdk/jdk-6u23/com/sun/xml/internal/ws/developer/JAXBContextFactory.html