Webservice-Client: Common approach with Spring WS, JAXB and just one WSDL file?
Date : March 29 2020, 07:55 AM
like below fixes the issue I would like to use Spring WS to build a Webservice-Client with JAXB for marshalling and unmarshalling the Java classes. , I can generate Java classes with JAXB (xbj.exe)
|
Is it possible to create a WS-client from WSDL file using Spring-WS? (It seems not)
Tag : java , By : Marianisho
Date : March 29 2020, 07:55 AM
hope this fix your issue I'd like to know if it's possible to create Web Services client from a WSDL file using Spring Web Services. , You can do it this way:
|
Accessing API exposed via wsdl in spring REST based client
Date : March 29 2020, 07:55 AM
With these it helps How can we access a wsdl of a soap project whose war is deployed on the same server, by a Rest based project using spring maven. Basically , I have to access an API that is exposed via wsdl and I have to access this API, the response than needs to be returned as json from a rest POST method. It will be like a REST post method, accepting the inputs and invoking this API (from wsdl) and manipulating the response as JSON, Hi I have used the following approach to implement the above requirement:
http://myshittycode.com/2013/10/01/using-spring-web-services-and-jaxb-to-invoke-web-service-based-on-wsdl/
1. changed the pom to add spring-ws dependency and plugin.
2. build the classes and it generated the classes from the wsdl.
3. changed the application xml :
<!--Generating web sources-->
<!-- Define the SOAP version used by the WSDL -->
<bean id="soapMessageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
<property name="soapVersion">
<util:constant static-field="org.springframework.ws.soap.SoapVersion.SOAP_12"/>
</property>
</bean>
<!-- The location of the generated Java files -->
<oxm:jaxb2-marshaller id="marshaller" contextPath="com.pb.pims.generatedsources"/>
<!-- Configure Spring Web Services -->
<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
<constructor-arg ref="soapMessageFactory"/>
<property name="marshaller" ref="marshaller"/>
<property name="unmarshaller" ref="marshaller"/>
<property name="defaultUri" value="http://localhost/HSWS/services/HSService?wsdl"/>
</bean>
4. Created the Service class;
@Service
public class HSService {
@Autowired
private WebServiceTemplate webServiceTemplate;
public List<HSChild> getHSChildren(String hscode, String country,String limit) {
GetHSChildren getHSChildren= new ObjectFactory().createGetHSChildren();
getHSChildren.setCountry(country);
getHSChildren.setHsCode(hscode);
getHSChildren.setLimit(Integer.parseInt(limit));
GetHSChildrenResponse response = (GetHSChildrenResponse) webServiceTemplate.marshalSendAndReceive(getHSChildren);
return response.getGetHSChildrenReturn();
}
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
HSService hsService = context.getBean(HSService.class);
}
}
So, I am able to call this aPI from the wsdl via my client. But I am always getting the values of the getGetHSChildrenReturn. hscode and getGetHSChildrenReturn.description as null.
Please find below the getGetHSChildrenReturn.class generated in the Step1 :
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"getHSChildrenReturn"
})
@XmlRootElement(name = "getHSChildrenResponse")
public class GetHSChildrenResponse {
@XmlElement(required = true)
protected List<HSChild> getHSChildrenReturn;
public List<HSChild> getGetHSChildrenReturn() {
if (getHSChildrenReturn == null) {
getHSChildrenReturn = new ArrayList<HSChild>();
}
return this.getHSChildrenReturn;
}
Also, I verified in the service code , which we are invoking via this wsdl by putting logging, that the correct request is going and it is returning the expected response at service end. But while coming to the client, the values are set as null.
Please help, what's wrong here in the client side.
Thanks in advance.
|
Writing a Spring Boot WSDL *client* that presents a certificate for authentication
Date : March 29 2020, 07:55 AM
will be helpful for those in need I am writing a server using Spring Boot v 1.2.7.RELEASE on Java 1.8. My code is configured entirely with annotations and currently has no XML other than the Maven pom. , Here's what I have, using Apache HttpClient 4.5.2: import java.io.IOException;
import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.Arrays;
import java.util.List;
import javax.net.ssl.SSLContext;
import org.apache.http.Header;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHeader;
import org.apache.http.ssl.SSLContexts;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.ws.WebServiceMessageFactory;
import org.springframework.ws.pox.dom.DomPoxMessageFactory;
import org.springframework.ws.transport.WebServiceMessageSender;
import org.springframework.ws.transport.http.HttpComponentsMessageSender;
import org.springframework.ws.transport.http.MessageDispatcherServlet;
@Configuration
class ApplicationIntegrationTestConfiguration {
@Value("${lcm.request.endpoint}")
private String endpointUri;
@Value("${lcm.request.keystorepath}")
private Resource keyStore;
@Value("${lcm.request.keystorepass}")
private char[] keyStorePass;
@Value("${lcm.request.keystoretype}")
private String keyStoreType;
@Value("${lcm.request.truststorepath}")
private Resource trustStore;
@Value("${lcm.request.truststorepass}")
private char[] trustStorePass;
@Value("${lcm.request.truststoretype}")
private String trustStoreType;
private static final String ACCEPT_HEADER_VALUE = "application/xml";
@Bean
public WebServiceMessageSender messageSender(
LayeredConnectionSocketFactory factory) throws Exception {
Header header = new BasicHeader(HttpHeaders.ACCEPT, ACCEPT_HEADER_VALUE);
List<Header> defaultHeaders = Arrays.asList(header);
CloseableHttpClient client = HttpClientBuilder.create()
.setSSLSocketFactory(factory)
.setDefaultHeaders(defaultHeaders)
.build();
HttpComponentsMessageSender messageSender = new HttpComponentsMessageSender(
client);
// needed if used as a standalone client
//messageSender.afterPropertiesSet();
return messageSender;
}
@Bean
public LayeredConnectionSocketFactory sslFactory() {
try {
final KeyStore keystore = KeyStore.getInstance(this.keyStoreType);
try (InputStream readStream = this.keyStore.getInputStream()) {
keystore.load(readStream, this.keyStorePass);
}
final KeyStore truststore = KeyStore.getInstance(this.trustStoreType);
try (InputStream readStream = this.trustStore.getInputStream()) {
truststore.load(readStream, this.trustStorePass);
}
SSLContext sslContext = SSLContexts
.custom()
.loadTrustMaterial(truststore, null)
.loadKeyMaterial(keystore, this.keyStorePass)
.build();
SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory(sslContext,
new DefaultHostnameVerifier()
);
return sslConnectionFactory;
} catch (KeyManagementException | UnrecoverableKeyException |
NoSuchAlgorithmException | KeyStoreException
| CertificateException | IOException e) {
throw new IllegalArgumentException(String.format("Problem with keystore %s or truststore %s",
this.keyStore, this.trustStore), e);
}
}
@Bean
public PingClient pingClient(Jaxb2Marshaller marshaller,
WebServiceMessageFactory messageFactory, WebServiceMessageSender messageSender) {
PingClient client = new PingClient();
client.setDefaultUri(this.endpointUri + "/Ping/v1");
client.setMarshaller(marshaller);
client.setUnmarshaller(marshaller);
client.setMessageFactory(messageFactory);
client.setMessageSender(messageSender);
return client;
}
// this bean is the key in selecting between SOAP and POX (plain old XML)
@Bean(name = MessageDispatcherServlet.DEFAULT_MESSAGE_FACTORY_BEAN_NAME)
public WebServiceMessageFactory messageFactory() {
return new DomPoxMessageFactory();
}
@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setPackagesToScan("my.packages");
return marshaller;
}
}
|
Spring Soap web service client using a WSDL and codehaus jaxb2; need XmlRootElement?
Date : March 29 2020, 07:55 AM
|