I need to exchange data with a SOAP service. This service provides many WSDL endpoints so I took one of them to generate Java code (I followed this tutorial: https://www.baeldung.com/maven-wsdl-stubs). Many classes were generated plus an interface and a service. Consider this code:
CodesService Interface:
package my.package.soapconsumer.models.service.getCodes;
import org.springframework.web.bind.annotation.RequestHeader;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;
/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.3.2
* Generated source version: 2.2
*
*/
#WebService(name = "CodesService", targetNamespace = "https://thewsdlprovider.xyz/")
#XmlSeeAlso({
ObjectFactory.class
})
public interface CodesService {
/**
*
* #param requestMethodOne
* #return
* returns my.package.soapconsumer.models.service.getCodes.ResponseMethodOne
*/
#WebMethod
#WebResult(name = "ResponseMethodOne", targetNamespace = "")
#RequestWrapper(localName = "methodOne", targetNamespace = "https://thewsdlprovider.xyz/", className = "my.package.soapconsumer.models.service.getCodes.MethodOne")
#ResponseWrapper(localName = "methodOneResponse", targetNamespace = "https://thewsdlprovider.xyz/", className = "my.package.soapconsumer.models.service.getCodes.MethodOneResponse")
public ResponseMethodOne methodOne(
#WebParam(name = "RequestMethodOne", targetNamespace = "")
RequestMethodOne requestMethodOne);
/**
*
* #return
* returns my.package.soapconsumer.models.service.getCodes.ResponseComunicacion
*/
#WebMethod
#WebResult(name = "ResponseTwo", targetNamespace = "")
#RequestWrapper(localName = "methodTwo", targetNamespace = "https://thewsdlprovider.xyz/", className = "my.package.soapconsumer.models.service.getCodes.VerificarComunicacion")
#ResponseWrapper(localName = "methodTwoResponse", targetNamespace = "https://thewsdlprovider.xyz/", className = "my.package.soapconsumer.models.service.getCodes.VerificarComunicacionResponse")
public ResponseComunicacion methodTwo();
/**
*
* #param requestMethodThreeApp
* #return
* returns my.package.soapconsumer.models.service.getCodes.ResponseMethodThree
*/
#WebMethod
#WebResult(name = "ResponseMethodThree", targetNamespace = "")
#RequestWrapper(localName = "methodThree", targetNamespace = "https://thewsdlprovider.xyz/", className = "my.package.soapconsumer.models.service.getCodes.MethodThree")
#ResponseWrapper(localName = "methodThreeResponse", targetNamespace = "https://thewsdlprovider.xyz/", className = "my.package.soapconsumer.models.service.getCodes.MethodThreeResponse")
public ResponseMethodThree methodThree(
#WebParam(name = "RequestMethodThreeApp", targetNamespace = "")
RequestMethodThreeApp requestMethodThreeApp);
/**
*
* #param requestMethodFour
* #return
* returns my.package.soapconsumer.models.service.getCodes.ResponseMethodFour
*/
#WebMethod
#WebResult(name = "ResponseMethodFour", targetNamespace = "")
#RequestWrapper(localName = "methodFour", targetNamespace = "https://thewsdlprovider.xyz/", className = "my.package.soapconsumer.models.service.getCodes.MethodFour")
#ResponseWrapper(localName = "methodFourResponse", targetNamespace = "https://thewsdlprovider.xyz/", className = "my.package.soapconsumer.models.service.getCodes.MethodFourResponse")
public ResponseMethodFour methodFour(
#WebParam(name = "RequestMethodFour", targetNamespace = "")
RequestMethodFour requestMethodFour);
/**
*
* more methods
*
*/
}
CodesService_Service Service
package my.package.soapconsumer.models.service.getCodes;
import org.springframework.web.bind.annotation.RequestHeader;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.WebServiceFeature;
/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.3.2
* Generated source version: 2.2
*
*/
#WebServiceClient(name = "CodesService", targetNamespace = "https://thewsdlprovider.xyz/Codes", wsdlLocation = "https://codes.thewsdlprovider.xyz/v2/Codes?wsdl")
public class CodesService_Service
extends Service
{
private final static URL CODES_SERVICE_WSDL_LOCATION;
private final static WebServiceException CODES_SERVICE_EXCEPTION;
private final static QName CODES_SERVICE_QNAME = new QName("https://thewsdlprovider.xyz/Codes", "CodesService");
static {
URL url = null;
WebServiceException e = null;
try {
url = new URL("https://codes.thewsdlprovider.xyz/v2/Codes?wsdl");
} catch (MalformedURLException ex) {
e = new WebServiceException(ex);
}
CODES_SERVICE_WSDL_LOCATION = url;
CODES_SERVICE_EXCEPTION = e;
}
public CodesService_Service() {
super(__getWsdlLocation(), CODES_SERVICE_QNAME);
}
public CodesService_Service(WebServiceFeature... features) {
super(__getWsdlLocation(), CODES_SERVICE_QNAME, features);
}
public CodesService_Service(URL wsdlLocation) {
super(wsdlLocation, CODES_SERVICE_QNAME);
}
public CodesService_Service(URL wsdlLocation, WebServiceFeature... features) {
super(wsdlLocation, CODES_SERVICE_QNAME, features);
}
public CodesService_Service(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public CodesService_Service(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
super(wsdlLocation, serviceName, features);
}
/**
*
* #return
* returns CodesService
*/
#WebEndpoint(name = "CodesServicePort")
public CodesService getCodesServicePort() {
return super.getPort(new QName("https://thewsdlprovider.xyz/Codes", "CodesServicePort"), CodesService.class);
}
/**
*
* #param features
* A list of {#link javax.xml.ws.WebServiceFeature} to configure on the proxy. Supported features not in the <code>features</code> parameter will have their default values.
* #return
* returns CodesService
*/
#WebEndpoint(name = "CodesServicePort")
public CodesService getCodesServicePort(WebServiceFeature... features) {
return super.getPort(new QName("https://thewsdlprovider.xyz/Codes", "CodesServicePort"), CodesService.class, features);
}
private static URL __getWsdlLocation() {
if (CODES_SERVICE_EXCEPTION!= null) {
throw CODES_SERVICE_EXCEPTION;
}
return CODES_SERVICE_WSDL_LOCATION;
}
public String getWsdlLocation(){
return CODES_SERVICE_WSDL_LOCATION.toString();
}
}
I need to use the methods or functions provided by the interface so to testing them I'm using a REST controller:
#RestController
#RequestMapping("/codes")
public class CodigosRestController {
#GetMapping
public String obtainCode(){
CodesService_Service service = new CodesService_Service();
String code = service.getCodesServicePort().methodTwo().toString();
return code;
}
}
But when I run it to query the REST endpoint I get the following error:
{
"timestamp": "2022-02-18T13:44:03.234+00:00",
"status": 500,
"error": "Internal Server Error",
"trace": "com.sun.xml.internal.ws.fault.ServerSOAPFaultException: Client received SOAP Fault from server: THE SERVICE REQUIRES API KEY Please see the server log to find more detail regarding exact cause of the failure.\n\tat com.sun.xml.internal.ws.fault.SOAP11Fault.getProtocolException(SOAP11Fault.java:178)\n\tat com.sun.xml.internal.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:116)\n\tat com.sun.xml.internal.ws.client.sei.StubHandler.readResponse(StubHandler.java:238)\n\tat ...
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1732)\n\tat org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)\n\tat org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)\n\tat org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)\n\tat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)\n\tat java.lang.Thread.run(Thread.java:748)\n",
"message": "Client received SOAP Fault from server: THE SERVICE REQUIRES API KEY Please see the server log to find more detail regarding exact cause of the failure.",
"path": "/codes"
}
It is necessary to send an API KEY in the headers but I don't know how.
So I used SoapUI to test the SOAP endpoint adding this header and I received a correct answer:
What I tried:
Use the WebServiceFeature... features constructor parameter but I
it seems that a header can't be added to this object. In fact, I
don't know what those three points mean.
Use the #RequestHeader but it reports that is not applicable to
web methods
So how can I add a header to my interface or service? Thanks in advance.
To work with the raw request you either need to create a request interceptor or just set the headers on the requested context on the service port. So in your case, it should be as simple as
#RestController
#RequestMapping("/codes")
public class CodigosRestController {
#GetMapping
public String obtainCode(){
CodesService_Service service = new CodesService_Service();
CodesService port = service.getCodesServicePort();
// generate the headers
Map<String, List<String>> requestHeaders = new HashMap<>();
requestHeaders.put("apiKey",Arrays.asList("TokeApi yourtokenhere"));
BindingProvider bindingProvider = (BindingProvider) port;
// set the headers on the request context
bindingProvider.getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS, requestHeaders);
String code = port.methodTwo().toString();
return code;
}
}
I am consuming a soap service using dropwizard-jaxws.
Managed to generate the relevant java classes using Apache CXF 3.4. Here's the generated interface IService.
#WebService(targetNamespace = "http://tempuri.org/", name = "IService")
#XmlSeeAlso({ObjectFactory.class})
#SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
public interface IService {
#WebMethod(operationName = "PayBill", action = "http://tempuri.org/IService/PayBill")
#Action(input = "http://tempuri.org/IService/PayBill", output = "http://tempuri.org/IService/PayBillResponse")
#WebResult(name = "PayBillResponse", targetNamespace = "http://tempuri.org/", partName = "parameters")
public PayBillResponse payBill(
#WebParam(partName = "parameters", name = "PayBill", targetNamespace = "http://tempuri.org/")
PayBill parameters
);
I wrote this implementing class IserviceImpl in kotlin
#WebService(endpointInterface = "com.mmm.platform.utility.easy.IService",
targetNamespace = "http://tempuri.org/",
name = "IService",
wsdlLocation = "wsdl/mmm-easy.wsdl")
class IServiceImpl: IService {
override fun payBill(parameters: PayBill?): PayBillResponse {
var response = PayBillResponse();
response.setSctrxnid(parameters?.sctxnid);
return response;
}
This is my dropwizard Application class in kotlin
class UtilityServiceApplication : Application<UtilityServiceConfiguration>() {
private val jaxWsBundle = JAXWSBundle<Any>()
override fun initialize(bootstrap: Bootstrap<UtilityServiceConfiguration>?) {
bootstrap?.addBundle(jaxWsBundle)
}
override fun run(configuration: UtilityServiceConfiguration?, environment: Environment?) {
environment!!.jersey().register(
AccessEasyServiceResource(
jaxWsBundle.getClient(
ClientBuilder(IService::class.java,
"http://mmm.hhhhh.xyz/EMTerminalAPI/wsdl.svc")
.handlers(AccessEasyClientHandler()))))
}
}
Dropwizard resource class AccessServiceResource in kotlin for end-point /paybillone
#Path("/paybillone")
#Produces(MediaType.APPLICATION_JSON)
public class AccessServiceResource {
var serviceClient: IService? = null
constructor(serviceClient: IService) {
this.serviceClient = serviceClient;
}
#GET
#Timed
fun getFoo(): String {
val of = ObjectFactory()
var payBill = of.createPayBill()
payBill.sctxnid = of.createPayBillSctxnid(RandomStringUtils.randomAlphanumeric(15))
payBill.txntype = of.createPayBillTxntype("1")
payBill.payeecode = of.createPayBillPayeecode("POWER")
payBill.accountno = of.createPayBillAccountno("04246483624")
payBill.phoneno = of.createPayBillPhoneno("0700252181")
payBill.amount = of.createPayBillAmount("5000")
var payBillResponse = serviceClient?.payBill(payBill)
return payBillResponse?.receiptno?.value.toString()
}
This call var payBillResponse = serviceClient?.payBill(payBill) unfortunately returned an error response message missing mandatory values yet I clearly added them to the payBill object.
Qn 1 Is this the correct way of creating the request object, so I get the correct corresponding response payBillResponse?
Qn 2 How and where should I pass payBill so I get the correct response to that call?
Qn 3 The other issue is most likely my implementation of IServiceImpl is wrong, is it even required?
java artifacts for Soap Web Service Client using wsimport from a WSDL. Which produced:
AppPortalSMupdate.java with some sample code from it
public interface AppPortalSMupdate {
#WebMethod(operationName = "RetrieveAppPortalSMupdate", action = "Retrieve")
#WebResult(name = "RetrieveAppPortalSMupdateResponse", targetNamespace = "http://schemas.hp.com/SM/7", partName = "RetrieveAppPortalSMupdateResponse")
public RetrieveAppPortalSMupdateResponse retrieveAppPortalSMupdate(
#WebParam(name = "RetrieveAppPortalSMupdateRequest", targetNamespace = "http://schemas.hp.com/SM/7", partName = "RetrieveAppPortalSMupdateRequest")
RetrieveAppPortalSMupdateRequest retrieveAppPortalSMupdateRequest);
AppPortalSMupdate_Service.java
#WebServiceClient(name = "AppPortalSMupdate", targetNamespace = "http://schemas.hp.com/SM/7", wsdlLocation ="http://ss_user:sqzblsft#msmapptst001.lvh.com:13088/SM/7/AppPortalSMupdate.wsdl")
public class AppPortalSMupdate_Service extends Service{
private final static URL APPPORTALSMUPDATE_WSDL_LOCATION;
private final static WebServiceException APPPORTALSMUPDATE_EXCEPTION;
private final static QName APPPORTALSMUPDATE_QNAME = new QName("http://schemas.hp.com/SM/7", "AppPortalSMupdate");
static {
URL url = null;
WebServiceException e = null;
try {
url = new URL("http://ss_user:sqzblsft#msmapptst001.lvh.com:13088/SM/7/AppPortalSMupdate.wsdl");
} catch (MalformedURLException ex) {
e = new WebServiceException(ex);
}
APPPORTALSMUPDATE_WSDL_LOCATION = url;
APPPORTALSMUPDATE_EXCEPTION = e;
This is the call to in my main from test client class:
AppPortalSMupdate appUpdate = calc.getAppPortalSMupdate();
UpdateAppPortalSMupdateResponse appResponse = appUpdate.updateAppPortalSMupdate(requestMessage);
My question is the web service needs a user id and password. How do I add a user id and password in my call in the main. This is being used inside a liferay portlet.
use #HandlerChain annotation and configure them by implementing the SOAPHandler interface.
Let me know if you need more clarification.
I have a web service using JAX-WS and Ibatis, when analyzed with the New Relic tool can see that the running time is 17 seconds. Seeing the detail of the report shows that the database calls are fast, but what is delaying is this: JAXWSWebAppServlet.service (). Someone has an idea that can be ??
Thanks x your help.
PD: I would like to know where to locate JAXWSWebAppServlet.service () in my code. Any ideas??
The WS code:
#WebService(name = "WS_XXXXXX", portName = "WS_XXXXXSoap12HttpPort")
#BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public class WS_GetPlanUsage {
#WebMethod(operationName = "xxxxxx")
#javax.jws.soap.SOAPBinding(parameterStyle = javax.jws.soap.SOAPBinding.ParameterStyle.WRAPPED)
public GetPlanUsageMapBean getPlanUsage(#WebParam(name = "phoneNumber", partName = "request") String phoneNumber,
#WebParam(name = "initialDate", partName = "request") String initialDate,
#WebParam(name = "finalDate", partName = "request") String finalDate) {
BReturn oBReturn = null;
GetPlanUsageResponse response = new GetPlanUsageResponse();
GetPlanUsageRequest rq = new GetPlanUsageRequest();
rq.setPhoneNumber(phoneNumber);
rq.setInitialDate(initialDate);
rq.setFinalDate(finalDate);
oBReturn = new GetPlanUsageBL().getPlanUsage(rq);
GetPlanUsageMapBean getPlanUsage = (GetPlanUsageMapBean)oBReturn.getObject();
return getPlanUsage;
}
I'm trying to implement a SOAP Web Service that generates a SOAP message for managing Address Book entries. The SOAP API is from Blackboard ConnectTxt and provides a single service -"addressbook" to maintain address book entries. This service permits developers to request additions, updates and deletes to an address book by creating an appropriate SOAP message.
Using Apache CXF 2.6.1 I've generated objects from their WSDL and a default impl (below)
#javax.jws.WebService(
serviceName = "TxttoolsAddressbookServiceService",
portName = "TxttoolsAddressbookServicePort",
targetNamespace = "http://www.txttools.co.uk/connectors/soap/addressbook/definitions",
wsdlLocation = "file:/C:/Users/me/Workspaces/work/bbtxttools/src/main/resources/wsdl/txttoolsAddressbook.wsdl",
endpointInterface = "uk.co.txttools.connectors.soap.addressbook.definitions.TxttoolsAddressbookService")
public class TxttoolsAddressbookServiceImpl implements TxttoolsAddressbookService {
private static final Logger LOG = Logger.getLogger(TxttoolsAddressbookServiceImpl.class.getName());
/* (non-Javadoc)
* #see uk.co.txttools.connectors.soap.addressbook.definitions.TxttoolsAddressbookService#addressbook(uk.co.txttools.connectors.soap.addressbook.schemas.AddressbookRequest addressbookRequest )*
*/
public uk.co.txttools.connectors.soap.addressbook.schemas.AddressbookResponse addressbook(uk.co.txttools.connectors.soap.addressbook.schemas.AddressbookRequest addressbookRequest) {
LOG.info("Executing operation addressbook");
System.out.println(addressbookRequest);
try {
uk.co.txttools.connectors.soap.addressbook.schemas.AddressbookResponse _return = null;
return _return;
} catch (java.lang.Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
}
The addressbook method simply returns an empty response. I'm baffled as to what should go in here. I would have expected to code some DAOs to provide data but the method appears to do nothing. I'm obviously missing a trick somewhere. The AddressBookService is included:
#WebService(targetNamespace = "http://www.txttools.co.uk/connectors/soap/addressbook/definitions", name = "TxttoolsAddressbookService")
#XmlSeeAlso({uk.co.txttools.connectors.soap.addressbook.schemas.ObjectFactory.class})
#SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
public interface TxttoolsAddressbookService {
#WebResult(name = "AddressbookResponse", targetNamespace = "http://www.txttools.co.uk/connectors/soap/addressbook/schemas", partName = "AddressbookResponse")
#WebMethod(operationName = "Addressbook")
public uk.co.txttools.connectors.soap.addressbook.schemas.AddressbookResponse addressbook(
#WebParam(partName = "AddressbookRequest", name = "AddressbookRequest", targetNamespace = "http://www.txttools.co.uk/connectors/soap/addressbook/schemas")
uk.co.txttools.connectors.soap.addressbook.schemas.AddressbookRequest addressbookRequest
);
}
Any suggestions would be appreciated.
Andrew