java - How to remove the namespace of SOAPHeaderElement -
i have java
class generate following xml
:
<v:envelope xmlns:v="http://schemas.xmlsoap.org/soap/envelope/"> <v:header> <b>lge nexus 5</b> <c>android</c> <d>6.0.1</d> <e>4.1.5</e> <f>127.0.0.1</f> <g/>?<g> <h>0.0</h> <i>0.0</i> <j"/> <k>8320738e-8634-4f73-a4dd-874a5d79e336</k> <l>2017-08-14 13:24:01</l> <m>8797e74f0d6eb7b1ff3dc114d4aa12d3</m> </v:header> <v:body> <ns2:getstatus xmlns:ns2="http://soap.ws.placa.service.sinesp.serpro.gov.br/"> <a>abc1234</a> </ns2:getstatus> </v:body> </v:envelope>
but current result is:
<v:envelope xmlns:v="http://schemas.xmlsoap.org/soap/envelope/"> <v:header> <b xmlns="http://schemas.xmlsoap.org/soap/envelope/">lge nexus 5</b> <c xmlns="http://schemas.xmlsoap.org/soap/envelope/">android</c> <d xmlns="http://schemas.xmlsoap.org/soap/envelope/">6.0.1</d> <e xmlns="http://schemas.xmlsoap.org/soap/envelope/">4.1.5</e> <f xmlns="http://schemas.xmlsoap.org/soap/envelope/">127.0.0.1</f> <g xmlns="http://schemas.xmlsoap.org/soap/envelope/">?</g> <h xmlns="http://schemas.xmlsoap.org/soap/envelope/">0.0</h> <i xmlns="http://schemas.xmlsoap.org/soap/envelope/">0.0</i> <j xmlns="http://schemas.xmlsoap.org/soap/envelope/" /> <k xmlns="http://schemas.xmlsoap.org/soap/envelope/">8320738e-8634-4f73-a4dd-874a5d79e336</k> <l xmlns="http://schemas.xmlsoap.org/soap/envelope/">2017-08-14 13:24:01</l> <m xmlns="http://schemas.xmlsoap.org/soap/envelope/">8797e74f0d6eb7b1ff3dc114d4aa12d3</m> </v:header> <v:body> <ns2:getstatus xmlns:ns2="http://soap.ws.placa.service.sinesp.serpro.gov.br/"> <a>abc1234</a> </ns2:getstatus> </v:body> </v:envelope>
my class
following:
import java.io.bytearrayoutputstream; import java.io.ioexception; import java.util.iterator; import javax.xml.bind.jaxbcontext; import javax.xml.bind.jaxbexception; import javax.xml.bind.marshaller; import javax.xml.bind.annotation.xmlelement; import javax.xml.bind.annotation.xmlrootelement; import javax.xml.bind.annotation.xmltransient; import javax.xml.namespace.qname; import javax.xml.parsers.documentbuilderfactory; import javax.xml.parsers.parserconfigurationexception; import javax.xml.soap.messagefactory; import javax.xml.soap.soapbody; import javax.xml.soap.soapenvelope; import javax.xml.soap.soapexception; import javax.xml.soap.soapheader; import javax.xml.soap.soapmessage; import org.w3c.dom.document; @xmlrootelement(name = "getstatus", namespace = "http://soap.ws.placa.service.sinesp.serpro.gov.br/") public class request { private string plate; private string device; private double latitude; private string operationalsystem; private string majorversion; private string minorversion; private string ip; private string token; private string uuid; private double longitude; private string date; private string hash; public request() { this.device = "lge nexus 5"; this.operationalsystem = "android"; this.majorversion = "6.0.1"; this.minorversion = "4.1.5"; this.ip = "127.0.0.1"; this.hash = "8797e74f0d6eb7b1ff3dc114d4aa12d3"; } @xmlelement(name = "a") public string getplate() { return plate; } public void setplate(string plate) { this.plate = plate; } @xmltransient public string getdevice() { return device; } public void setdevice(string device) { this.device = device; } @xmltransient public double getlatitude() { return latitude; } public void setlatitude(double latitude) { this.latitude = latitude; } @xmltransient public string getoperationalsystem() { return operationalsystem; } public void setoperationalsystem(string operationalsystem) { this.operationalsystem = operationalsystem; } @xmltransient public string getmajorversion() { return majorversion; } public void setmajorversion(string majorversion) { this.majorversion = majorversion; } @xmltransient public string getminorversion() { return minorversion; } public void setminorversion(string minorversion) { this.minorversion = minorversion; } @xmltransient public string getip() { return ip; } public void setip(string ip) { this.ip = ip; } @xmltransient public string gettoken() { return token; } public void settoken(string token) { this.token = token; } @xmltransient public string getuuid() { return uuid; } public void setuuid(string uuid) { this.uuid = uuid; } @xmltransient public double getlongitude() { return longitude; } public void setlongitude(double longitude) { this.longitude = longitude; } @xmltransient public string getdate() { return date; } public void setdate(string date) { this.date = date; } @xmltransient public string gethash() { return hash; } public void sethash(string hash) { this.hash = hash; } public string toxml() { document document; marshaller marshaller; soapmessage soapmessage; bytearrayoutputstream outputstream; string output; try { document = documentbuilderfactory.newinstance().newdocumentbuilder().newdocument(); marshaller = jaxbcontext.newinstance(request.class).createmarshaller(); marshaller.marshal(this, document); soapmessage = messagefactory.newinstance().createmessage(); this.fillenvelope(soapmessage.getsoappart().getenvelope()); this.fillbody(soapmessage.getsoapbody(), document); this.fillheaders(soapmessage.getsoapheader()); outputstream = new bytearrayoutputstream(); soapmessage.writeto(outputstream); output = new string(outputstream.tobytearray()); } catch (ioexception | jaxbexception | parserconfigurationexception | soapexception ex) { throw new runtimeexception(ex); } return output; } private void fillenvelope(soapenvelope envelope) { iterator prefixes; envelope.setprefix("v"); prefixes = envelope.getnamespaceprefixes(); while (prefixes.hasnext()) { string prefix = (string) prefixes.next(); envelope.removenamespacedeclaration(prefix); } } private void fillheaders(soapheader soapheader) throws soapexception { soapheader.setprefix("v"); soapheader.addheaderelement(new qname(soapheader.getnamespaceuri(), "b")).setvalue(this.device); soapheader.addheaderelement(new qname(soapheader.getnamespaceuri(), "c")).setvalue(this.operationalsystem); soapheader.addheaderelement(new qname(soapheader.getnamespaceuri(), "d")).setvalue(this.majorversion); soapheader.addheaderelement(new qname(soapheader.getnamespaceuri(), "e")).setvalue(this.minorversion); soapheader.addheaderelement(new qname(soapheader.getnamespaceuri(), "f")).setvalue(this.ip); soapheader.addheaderelement(new qname(soapheader.getnamespaceuri(), "g")).setvalue(this.token); soapheader.addheaderelement(new qname(soapheader.getnamespaceuri(), "h")).setvalue(string.valueof(this.longitude)); soapheader.addheaderelement(new qname(soapheader.getnamespaceuri(), "i")).setvalue(string.valueof(this.latitude)); soapheader.addheaderelement(new qname(soapheader.getnamespaceuri(), "j")).setvalue(""); soapheader.addheaderelement(new qname(soapheader.getnamespaceuri(), "k")).setvalue(this.uuid); soapheader.addheaderelement(new qname(soapheader.getnamespaceuri(), "l")).setvalue(this.date); soapheader.addheaderelement(new qname(soapheader.getnamespaceuri(), "m")).setvalue(this.hash); } private void fillbody(soapbody soapbody, document document) throws soapexception { soapbody.adddocument(document); soapbody.setprefix("v"); } }
the test method is:
public static void main(string[] args) { request request = new request(); request.settoken("?"); request.setlatitude(0.0); request.setlongitude(0.0); request.setuuid(uuid.randomuuid().tostring()); // rfc 4122 class 4 random uuid request.setdate("2017-08-14 13:24:01"); request.setplate("abc1234"); system.out.println(request.toxml()); }
what need in order remove namespace
attribute of soapheaderelement
?
each soap header block (immediate child of header
element) must according soap specification namespace qualified. intended xml, hence violate soap specification.
you can of course use other means or plain xml libraries create xml want, when using soapheader.addheaderelement
, jax-rs implementation (at least in oracle's jre) verifies requirement , throws exception if try add header block empty namepsace.
Comments
Post a Comment