简体   繁体   中英

remove whitespaces from jaxb xml response

I have a WS that generates a SOAP XML message. Due to size limitations I would like to remove the unnecessary whitespaces (used for indentation) and new lines. How can I do this when using generated classes and annotations ( @WebService and @WebMethod )? In the examples I have seen it is done like this:

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

However, I am not manually creating the Marshaller so I do not know where I can add this property and if this is the correct way of doing it. The JAXB implementation is axis2 .

Create a custom JAXBContext and annotate your webservice as mentioned below:

    @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;
       }  
    }

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

<dataFormats><jaxb prettyPrint=false></dataFormats>这个漂亮的打印标志将以压缩方式格式化 xml 文件并删除 crlf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM