Defining multiple endpoints for multiple soap web services in spring boot application - spring-boot

I have a servlet mapping issue in spring boot application with multiple soap web services. I have two (or more) WebServices with different mappings.
Service A -> Endpoint1
Service B -> Endpoint2
Once I deploy spring boot application with two MessageDispatcherServlets , Service A and B both can be accessed with Endpoint1 only. I don't know how I can map Endpoint1 url to ServiceA and Endpoint2 to ServiceB.
Please check sample of my code for Service-A. Code is similar for Service-B.
#Bean(name = "ServiceA")
public Wsdl11Definition wsdl11DefinitionImportAgent() {
SimpleWsdl11Definition simpleWsdl11Definition = new SimpleWsdl11Definition();
simpleWsdl11Definition.setWsdl(new ClassPathResource("/wsdl/ServiceA.wsdl"));
return simpleWsdl11Definition;
}
#Bean
public ServletRegistrationBean messageDispatcherServletServiceA(ApplicationContext
applicationContext) {
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(applicationContext);
servlet.setTransformWsdlLocations(true);
return new ServletRegistrationBean(servlet, "/ws/ServiceA");
}

Related

ServletRegistrationBean in Spring cloud

I have recently migrated the application to Spring Cloud Gateway from zuul filter for routing to multiple microservices. I have a filter which is written in
native code (HTTPServlet). In Spring application initialization, I am registering that Servlet as below
#Bean
public ServletRegistrationBean oAuthCodeReceiverServletBean() {
ServletRegistrationBean oauthCodeReceiverServletBean = new ServletRegistrationBean(
new OAuthCodeRecieverServlet(), "/login/oauth/codereceiver");
return oauthCodeReceiverServletBean;
}
#Bean
public FilterRegistrationBean<OAuthFilter> securityRegistrationBean() {
FilterRegistrationBean<OAuthFilter> registrationBean = new FilterRegistrationBean<>(oAuthFilter);
registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
registrationBean.addUrlPatterns("/*");
return registrationBean;
}
But the Filter is not at all called. The filters that implements GlobalFilter are called. Is there a solution handle ?
Please note I dont have option to update the native code OauthFilter.

Spring boot MessageDispatcherServlet overriding DispatcherServlet. how to skip overriding and register both DispatcherServlet?

in a single application, I m trying to use soap as well as rest web service. and each servlet is given different URI, below is the code of config class.
The question is: only soap service URL is working fine but for the rest on getting 405 error.
#EnableWs
#Configuration
public class WebServiceConfig {
#Bean
public ServletRegistrationBean RsRegistrationBean(ApplicationContext applicationContext) {
DispatcherServlet servlet = new DispatcherServlet();
servlet.setApplicationContext(applicationContext);
return new ServletRegistrationBean(servlet,"/rest/*");
}
#Bean
public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServlet(
ApplicationContext context) {
MessageDispatcherServlet messageDispatcherServlet = new MessageDispatcherServlet();
messageDispatcherServlet.setApplicationContext(context);
messageDispatcherServlet.setTransformWsdlLocations(true);
return new ServletRegistrationBean<>(messageDispatcherServlet,"/");
}
}
Remove your WebServiceConfig class, as both are auto-configured by Spring Boot already (as of Spring Boot 1.4). Add the following to your application.properties
spring.mvc.servlet.path=/rest
spring.webservices.path=/
Now you leverage the Spring Boot proivded infrastructure instead of fighting with it.

Multiple Spring WS Endpoints on different URL's

I'm using Spring Boot and like in the example https://spring.io/guides/gs/producing-web-service/
WS Endpoints are exposed on /ws/* with MessageDispatcher:
#Bean
public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(applicationContext);
servlet.setTransformWsdlLocations(true);
return new ServletRegistrationBean(servlet, "/ws/*");
}
I want to have 2 endpoints on different URL's. Is that possible?

Java configuration for SAAJInInterceptor and WSS4JInInterceptor beans

I have a web application that provides a soap web service using apache cxf and spring boot. So I have no xml file in my classpath to configure my beans, they are all configured in java configuration.I would like to set up authentication with ws-security from cxf.
So I would like to know if there are java configurations for the SAAJInInterceptor and WSS4JInInterceptor beans for example like this:
#Bean
public ServletRegistrationBean cxfServlet() {
ServletRegistrationBean servlet = new ServletRegistrationBean(new CXFServlet(), "/services/*");
servlet.setLoadOnStartup(1);
return servlet;
}
You just have to add the interceptor when you create your endpoint :
Map<String,Object> interceptorConfig = new HashMap<String,Object>();
// set the properties of you WSS4J config
// ...
WSS4JInInterceptor myInterceptor = new WSS4JInInterceptor(interceptorConfig);
myEndpoint.getInInterceptors().add(myInterceptor);
Another solution is by using annotation with #InInterceptors

How to find the togglz url when integrating with spring-boot and jersey

My application based on spring-boot and jersey. I have configured togglz in my application. I can successfully launch my web application but running gradle bootRun. From the output during the startup, I am able to see below log message. But I am not able to access http://localhost:8080/togglz. My application root path is "/" which is managed by jersey. It seems that spring-mvc works well with togglz but failed to access it when integrating with jersey. What should I do in order to let jersey to accept the url?
2016-03-30 18:40:35.191 INFO 81748 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/togglz || /togglz.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
For Jersey 2 to work along with Spring MVC endpoints in a Spring Boot application, I would suggest to make sure your application.yml (or .properties makes such distinction, something like:
...
# Spring MVC dispatcher servlet path. Needs to be different than Jersey's to enable/disable Actuator endpoints access (/info, /health, ...)
server.servlet-path: /
# Jersey dispatcher servlet
spring.jersey.application-path: /api
...
You could read more about this at my blog: http://tech.asimio.net/2016/04/05/Microservices-using-Spring-Boot-Jersey-Swagger-and-Docker.html#implement-api-endpoints-using-jersey
If you are integrating Jersey 1 with Spring MVC endpoints in a Spring Boot app, Spring Boot doesn't provide a jersey 1 starter so everything needs to be "manually" configured, but basically if your Jersey servlet is mapped to "/", you would need to configure it to let pass 404 to the servlet container for further handling (maybe a Spring MVC or plain servlet endpoint). Something like:
Jersey 1 resources configuration:
#ApplicationPath("/")
public class DemoResourcesConfig extends PackagesResourceConfig {
private static final Map<String, Object> properties() {
Map<String, Object> result = new HashMap<>();
result.put(PackagesResourceConfig.PROPERTY_PACKAGES, "com.sun.jersey;com.asimio.api.demo1.rest");
// To forward non-Jersey paths to servlet container for Spring Boot actuator endpoints to work.
result.put("com.sun.jersey.config.feature.FilterForwardOn404", "true");
result.put(JSONConfiguration.FEATURE_POJO_MAPPING, "true");
return result;
}
public DemoResourcesConfig() {
super(properties());
}
...
}
Jersey 1 resource implementation:
package com.asimio.api.demo1.rest;
...
#Component
#Path("/actors")
#Produces(MediaType.APPLICATION_JSON)
public class ActorResource {
#GET
public List<Actor> findActors() {
...
}
#GET
#Path("{id}")
public Actor getActor(#PathParam("id") String id) {
...
}
...
}
ServletContextInitializer bean
#Bean
public ServletContextInitializer servletInitializer() {
return new ServletContextInitializer() {
#Override
public void onStartup(ServletContext servletContext) throws ServletException {
final ServletRegistration.Dynamic appServlet = servletContext.addServlet("jersey-servlet", new SpringServlet());
Map<String, String> filterParameters = new HashMap<>();
// Set filter parameters
filterParameters.put("javax.ws.rs.Application", "com.asimio.api.demo1.config.DemoResourcesConfig");
appServlet.setInitParameters(filterParameters);
appServlet.setLoadOnStartup(2);
appServlet.addMapping("/*");
}
};
}
application.yml
...
# For Spring MVC to enable Endpoints access (/admin/info, /admin/health, ...) along with Jersey
server.servlet-path: /admin
...
More about Jersey 1 and Spring Boot / Cloud could be found at my blog: http://tech.asimio.net/2016/11/14/Microservices-Registration-and-Discovery-using-Spring-Cloud-Eureka-Ribbon-and-Feign.html#create-the-demo-service-1

Resources