Implementing XSD's Exception Classes in Spring Web Service (SOAP) - spring-boot

I'm creating a SOAP web service in Spring Boot using Spring WS. The service I am recreating is a clone of a Service running on older technology which uses JavaX JWS, with the classes generated from an XSD.
The issues I have are linked/the same really:
1. I can't seem to 'receive' Exceptions thrown in the 'new' Web Service back to the consumer.
2. When querying the endpoint in SOAPUI, the Exception class for the Web Service appears as if it is a one-way method.
I believe this is because Spring WS isn't correctly implementing/understanding the Exception classes.
When an exception is thrown in the new service, the consumer receives a com.sun.xml.internal.ws.fault.ServerSOAPFaultException, rather than the Exception thrown in the Web Service.
The original JavaX service returns (using TestException as an example):
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope">
<faultcode>S:Server</faultcode>
<faultstring>Test</faultstring>
<detail>
<ns2:TestException xmlns:ns2="http://test.co.uk/">
<code>abc123</code>
<message>Test</message>
</ns2:TestException>
</detail>
</S:Fault>
</S:Body>
</S:Envelope>
I can't find any documentation or guidance on how the exception should be build/configured to produce the same <ns2> nested element. Is there a way to accurately pass the exception back so that it appears the same as it would in the service?

Here are some examples of implementing custom exception resolver which extends AbstractEndpointExceptionResolver and defines SOAP fault mapping for different exception types: example 1, example 2, example 3.

Related

Cannot show the endpoint list when calling link without ?wsdl, but still can call the SOAP web service?

I have a web service with Spring and CXF like:
#Service
#WebService(serviceName = "ReceiveMultiFromVOfficeImpl")
#RequiredArgsConstructor
public class ReceiveMultiFromVOfficeImpl {
public Long returnSignReult(ResultObj resultObj) {
log.info("Start receive from Voffice...");
return 0L;
}
}
and my ws-endpoint-lazy-load.xml file config:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans">
<bean class="com.abc.fw.ws.CxfWsEndpointFactory">
<property name="endpoints">
<list>
<value>com.abc.bccs2.service.TestCustomService</value>
<value>com.abc.voffice.vo.ReceiveMultiFromVOfficeImpl</value>
</list>
</property>
</bean>
</beans>
When I call http://localhost:8082/services/pbh/ReceiveMultiFromVOfficeImpl?wsdl, it works and I can call SOAP. But when I call http://localhost:8082/services/pbh/ReceiveMultiFromVOfficeImpl, I expected it will show the endpoint's information. It shows error:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring>Fault occurred while processing.</faultstring>
</soap:Fault>
</soap:Body>
</soap:Envelope>
What I want is something like:
How do I fix that?
I'm not sure you understand how a SOAP web service works, so here is some explanation about what you are seeing when accessing those URLs.
When dealing with a SOAP web service there is only one endpoint address to use to send messages to the web service and it is invoked over POST.
In your case:
POST http://localhost:8082/services/pbh/ReceiveMultiFromVOfficeImpl
is how you invoke the web service. As long as the web service is running and listening on that address, then a POST with a SOAP message will invoke the web service.
Because a SOAP web service can be described by a WSDL document, there must be some way to obtain the WSDL for the web service. By convention, it is accessible over a GET request at the web service endpoint by specifying a ?wsdl parameter.
In your case:
GET http://localhost:8082/services/pbh/ReceiveMultiFromVOfficeImpl?wsdl
will return the WSDL of the web service.
Any other GET/POST/URL comination will either return an error or will return some other information provided for convenience, assuming the web service framework used to build the web service does that. For example, this web service (http://www.dneonline.com/calculator.asmx) allows you to call its endpoint over GET and will return a human readable summary of what the WSDL accessible at (http://www.dneonline.com/calculator.asmx?wsdl) contains, but don't expect that to be the case with every framework out there (from what I remember CXF just returns an error if you call the web service endpoint over GET without a ?wsdl parameter).
So, to recap, the HTTP verbs and URLs valid for your web service are:
POST http://localhost:8082/services/pbh/ReceiveMultiFromVOfficeImpl -> to invoke the service
GET http://localhost:8082/services/pbh/ReceiveMultiFromVOfficeImpl?wsdl -> to obtain the service WSDL
Some other resources to read:
How is a SOAP call possible without WSDL?
The WSDL is not the SOAP web service
Is it mandatory to have a WSDL definition accessible using ?wsdl?
Finally, if you are using the proper URL and HTTP verb combination and you are getting that error, note that Fault occurred while processing is a generic error message that may show some issue with the web service code. Maybe there is a NullPointerException or some other error in your web service code. You need to look in your web service logs or in your web service console to see if there are any other exception messages or stacktrace that points to a root cause for that fault message.

How to consume Soap Rpc encoded in spring boot?

Hello I managed to generate classes from wsdl using axis 1.4.But I am stuck at invoking services. I tried to used WebServiceTemplate and jaxb2marshaller but there is no request class generated. So I tried created request class manually based on the generated response class. But it is getting any response from server.It says xmlrootelement is not found. So i think jaxb2marshaller is not the correct way to invoke service since it is old rpc encoded soap wsdl. Suggest me the idea to invoke service.
Update:
when using axis 1.4
I found the solution to invoke methods.It is simple 2 line code.
SomeService service=new SomeServiceLocator().getSomeServicePort();
Sample sample=service.fetchSample();

Consume SOAP Web services from spring with security header

I'm getting deprecated message for the bean Wss4jSecurityInterceptor. I'm using >spring-boot-starter-ws(1.4.7-RELEASE). spring-ws-security(2.4.4-RELEASE), org.apache.ws.security wss4j (1.6.9).
How to add the username token to consume SOAP services using Spring WebService Template ?
Sorry..Silly error..Need to upgrade to "org.springframework.ws.soap.security.wss4j2.Wss4jSecurityInterceptor;"...wss4j2 instead of wss4j.
However still Null pointer exception " Received Fault message for request Exception=null.
Settled. The application message exception object isn't extracted properly from the SoapFaultClientException.. e.getSoapFault().getFaultDetail()

Server side Exception or error to be propagated to JSP in Spring

I am trying to show an custom error message on any occurrence of exception or error in my business layer. I am catching the exception in my controller and I would like to display it in my JSP.
This exception or error is not associated with any field in the screen, it's a pure server exception. I am also using an Annotated Controller. I am using Prototype for making AJAX requests to my controller.
In Spring you can register a HandlerExceptionResolver which will catch exceptions thrown by your Spring MVC controllers and forward them to the view layer for rendering. These are described in the Spring docs here. Start with the SimpleMappingExceptionResolver (see javadoc) which gives a simple mechanism for mapping exception types to views.
However, if the exception occurs outside if your Spring controller, for whatever reason, then you'll need a more generic fall-back solution, which involves configuring error pages in your web.xml file. This is not Spring-specific. See here for an example of how to do it.

How to configure spring HandlerExceptionResolver to handle NullPointerException thrown in jsp?

From a jsp is thrown a NullPointerException for example using <% null.toString(); %>
This exception is not handled by the HandlerExceptionResolver, but thrown to the web container(tomcat) and converted into a code 500 error.
How can I configure spring to get that error in my HandlerExceptionResolver ?
Details:
Spring can be configured to handle exceptions thrown inside Controllers, but not exceptions thrown by view.
Of course i can resolve the NullPointerException, but i want to design a solution that will gracefully resolve any possible problem on the web application in order to display a user friendly message to the user.
See the HandlerInterceptor interface instead. You'll want the afterCompletion method. You can then intercept the response and then set the appropriate header information to redirect to a container-configured error web page. You're right that Spring doesn't have this functionality, this is going to have to be specified by the web.xml which determines which codes map to which pages.
I have not worked with this particular bit of the spring framework, but the docs say
"Interface to be implemented by objects than can resolve exceptions thrown during handler mapping or execution, in the typical case to error views. Implementors are typically registered as beans in the application context.
Error views are analogous to the error page JSPs, but can be used with any kind of exception including any checked exception, with potentially fine-granular mappings for specific handlers."
so I'd imagine that given that NullPointer extends RuntimeException the framework isn't designed to catch it. Is there a reason the exception(s) can't be handled in the controller directly?

Resources