简体   繁体   中英

No data from SOAP call in Java Jaxb2

I've unmarshalled my wsdl, and placed the generated sources under the package: generated.person

marshaller setup:

@Configuration
public class MarshalConfig {
@Bean
public Jaxb2Marshaller marshaller() {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    //marshaller.setContextPath("generated.person");
    marshaller.setPackagesToScan("generated.person");
    return marshaller;
}

@Bean
public RKIClient rkiClient(Jaxb2Marshaller marshaller) {
    RKIClient client = new RKIClient();
    client.setDefaultUri("https://uat.ws2.rki.dk/person.asmx");
    client.setMarshaller(marshaller);
    client.setUnmarshaller(marshaller);
    return client;
}

}

My spring boot call:

public SoegPersonCprResponse getPersonWithCPR(String cpr) {
    String retVal = "";
    SoegPersonCpr request = new SoegPersonCpr();
    request.setBrugernavn(RKIClient.RKI_USERNAME);
    request.setPassword(RKIClient.RKI_PASSWORD);
    request.setCPRnummer(cpr);
    SoegPersonCprResponse response = (SoegPersonCprResponse) getWebServiceTemplate()
            .marshalSendAndReceive("https://uat.ws2.rki.dk/person.asmx",
                    request,
                    new SoapActionCallback("http://webservices.rki.dk/SoegPersonCpr"));

    return response;
}

response from the call is: (JSON due to the @responsebody in the webservice)

"soegPersonCprResult": {
"error": null,
"person": null,
"adresser": null
}

But when I input the same data in SOAPui i get the following returned:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <SoegPersonCprResponse xmlns="http://webservices.rki.dk/">
         <SoegPersonCprResult>
            <Error xsi:type="XmlErrorOk" xmlns="http://webservices.rki.dk">
               <Code>0</Code>
               <Text>Ok</Text>
            </Error>
            <Person stamkortNr="3940982" xmlns="http://webservices.rki.dk">
               <Navnedata xsi:type="PersonNavnData" beskyttet="N">
                  <Fornavne>REAL LIFE NAME</Fornavne>
                  <Efternavn>LAST NAME</Efternavn>
               </Navnedata>
               <Adresse udenlandsk="N" beskyttet="N" VejNr="1671">
                  <PostDistrikt>
                     <Navn>ADRESS</Navn>
                     <PostNr>7777</PostNr>
                  </PostDistrikt>
                  <Kommune>
                     <Kode>766</Kode>
                  </Kommune>
                  <HusNr fraNr="5"/>
                  <Vej>wildroad</Vej>
                  <Dato>27082012</Dato>
               </Adresse>
               <Foedselsdato>15111996</Foedselsdato>
               <CprStatus>AKT</CprStatus>
               <Kreditadvarsel AendretDato="23032017">J</Kreditadvarsel>
            </Person>
            <Adresser xmlns="http://webservices.rki.dk">
               <StatusKode>1</StatusKode>
               <CodeMoniker>EksaktSvar</CodeMoniker>
            </Adresser>
         </SoegPersonCprResult>
      </SoegPersonCprResponse>
   </soap:Body>
</soap:Envelope>

Config:

String uri = "https://uat.ws2.rki.dk/person.asmx";

Jaxb2Marshaller jaxb2 = new Jaxb2Marshaller();
jaxb2.setContextPath(request.getClass().getPackage().getName());
WebServiceTemplate ws = getWebServiceTemplate();
ws.setMarshaller(jaxb2);
ws.setUnmarshaller(jaxb2);

And the interceptor with the call itself :

    try {

        final StringBuffer sb = new StringBuffer();

        ClientInterceptor clientInterceptor = new ClientInterceptor() {

            @Override
            public boolean handleResponse(MessageContext messageContext) throws WebServiceClientException {
                WebServiceMessage message = messageContext.getResponse();
                ByteArrayOutputStream bytArrayOutputStream = new ByteArrayOutputStream();
                try {
                    message.writeTo(bytArrayOutputStream);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String test = new String(bytArrayOutputStream.toByteArray());
                sb.append(test);
                // System.out.println(test);
                return true;
            }

            @Override
            public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
                // TODO Auto-generated method stub
                return true;
            }

            @Override
            public boolean handleFault(MessageContext messageContext) throws WebServiceClientException {
                // TODO Auto-generated method stub
                return true;
            }
        };
        ClientInterceptor[] interceptors = interceptors = new ClientInterceptor[1];
        interceptors[0] = clientInterceptor;
        ws.setInterceptors(interceptors);
        final StringBuffer sbuffer = new StringBuffer();                   

        SoegPersonCprResponse response =  (SoegPersonCprResponse) ws.marshalSendAndReceive(uri, request,
            new WebServiceMessageCallback() {
                @Override
                public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException {
                    ByteArrayOutputStream os = new ByteArrayOutputStream();
                    message.writeTo(os);
                    sbuffer.append(new String(os.toByteArray()));
                }
            });
     } catch (Exception e) {
        throw e;
     }

PS: When creating an object from type SoegPersonCprResponse or other that are generated from Jaxb I suggest using ObjectFactory

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