2013-05-21 123 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