Jax-WS - To Remove Empty Tags from Request XML

前端 未结 3 1712
后悔当初
后悔当初 2021-01-03 07:10

I\'m trying to consume a web service exposed by a provider. The Provider has a strict checking at his end that the request xml should not contain tags which don\'t have valu

3条回答
  •  醉梦人生
    2021-01-03 08:09

    This worked for me, when serializing a class where I couldn't use annotations (LogRecord):

                JAXBContext jc = JAXBContext.newInstance(LogRecord.class);
                JAXBElement je = new JAXBElement<>(new QName("log"), LogRecord.class, record);
                Marshaller marshaller = jc.createMarshaller();
                marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                marshaller.setAdapter(new XmlAdapter() {
                    @Override
                    public String marshal(String v) throws Exception {
                        if(null == v || v.length() == 0) {
                            return null;
                        }
                        return v;
                    }
                    @Override
                    public String unmarshal(String v) throws Exception {
                        return v;
                    }
                });
                ByteArrayOutputStream xml = new ByteArrayOutputStream();
                marshaller.marshal(je, xml);
    

提交回复
热议问题