How to configure multiple webservices using applicationContext.xml and CXF - spring

I have a applicationContext.xml in which I am configuring all my webservices like below -
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxrs:server id="restContainer" address="/">
<jaxrs:serviceBeans>
<ref bean="bookService" />
<ref bean="movieService" />
</jaxrs:serviceBeans>
</jaxrs:server>
<bean id="bookService" class="com.informit.apachecxfexample.BookServiceImpl"/>
<bean id="movieService" class="com.informit.apachecxfexample.MovieServiceImpl"/>
However only the first service (bookService) can be called . If I change the order of reference bean to
<ref bean="movieService" />
<ref bean="bookService" />
in the above case only movieService is called .
Is there a way of calling all the services configured in applicationContext.xml ?

Related

cxf rest multiple endpoints

I have generated the java classes from a wadl file with cxf. There are 3 resources definded an so 3 service classes with #PATH annotation are created. Now I want to publish them to the same url but I'm not sure how to achieve this.
Here are the snippets of the classes and wadl. The last part shows the beans.xml - At this point this is the only way I know how to publish the endpoints. Is there anothher way and how can I publish these 3 classes to the base url "/" and then they should match to the paths related to the annotations. Maybe a wrapper class for all but I'm not sure?
classes
#Path("status")
public class Status {
...
#Path("status/{id}")
public class StatusId {
...
#Path("counters")
public class Counters{
...
wadl
<resources base="http:localhost:8080/rest">
<resource path="status/{id}" id="status">
<method name="GET" id="getStatusById">
...
<resource path="status" id="status">
<method name="GET" id="getStatusByQueryParam">
...
</resource>
<resource path="counters" id="counters">
<method name="PUT" id="putCounters">
...
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">
<!-- do not use import statements if CXFServlet init parameters link to this beans.xml -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxrs:server id="statusService" address="/">
<jaxrs:serviceBeans>
<ref bean="status" />
</jaxrs:serviceBeans>
</jaxrs:server>
<!-- causes error
<jaxrs:server id="statusServiceId" address="/">
<jaxrs:serviceBeans>
<ref bean="statusId" />
</jaxrs:serviceBeans>
</jaxrs:server>
<jaxrs:server id="counterServiceId" address="/">
<jaxrs:serviceBeans>
<ref bean="counters" />
</jaxrs:serviceBeans>
</jaxrs:server>
-->
<bean id="status" class="package.Status"/>
<bean id="statusId" class="package.StatusId"/>
<bean id="counters" class="package.Counters"/>
</beans>
for geting multiple endpoints your
beans.xml should be looks like bellow
<jaxrs:server id="statusService" address="/">
<jaxrs:serviceBeans>
<ref bean="status" />
<ref bean="statusId" />
<ref bean="counters" />
</jaxrs:serviceBeans>
</jaxrs:server>

spring mvc validation meesage param

I'm using spring mvc 4 , and there is a problem with message params in validation messages.
for example the output of #Size is : size must be between min and max.
and key bundle in hibernate validator is size must be between {min} and {max}.
this is my spring mvc config :
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="net.devk"
use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.tiles3.TilesViewResolver" />
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="checkRefresh" value="true"/>
<property name="useMutableTilesContainer" value="true"/>
<property name="definitions">
<list>
<value>/WEB-INF/views/**/view.xml</value>
<value>/WEB-INF/layouts/layouts.xml</value>
</list>
</property>
</bean>
<!-- Configures the #Controller programming model -->
<!-- enables the support of annotation configuration for Spring MVC controllers,
as well as enabling Spring 3 type conversion and formatting support. Also,
support for JSR-303 Bean Validation is enabled by this tag -->
<mvc:annotation-driven validator="validator" />
<bean id="themeChangeInterceptor"
class="org.springframework.web.servlet.theme.ThemeChangeInterceptor">
<property name="paramName" value="theme" />
</bean>
<!-- Changes the locale when a 'locale' request parameter is sent; e.g.
/?locale=de -->
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<!-- Configures Handler Interceptors -->
<mvc:interceptors>
<ref bean="localeChangeInterceptor" />
<ref bean="themeChangeInterceptor" />
</mvc:interceptors>
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources/ directory -->
<!-- <mvc:resources mapping="/static/**" location="/static/" /> -->
<!-- defines the static resources (for example, CSS, JavaScript, images,
and so on) and their locations so Spring MVC can improve the performance
in serving those files -->
<mvc:resources location="/resources/" mapping="/resources/**" />
<!-- Enables the mapping of the DispatcherServlet to the web application’s
root context URL, while still allowing static resource requests to be handled
by the container's default servlet -->
<mvc:default-servlet-handler />
<!-- Saves a locale change using a cookie -->
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="cookieName" value="locale" />
</bean>
<bean
class="org.springframework.ui.context.support.ResourceBundleThemeSource"
id="themeSource">
<property name="basenamePrefix" value="theme.theme-" />
</bean>
<bean class="org.springframework.web.servlet.theme.CookieThemeResolver"
id="themeResolver">
<property name="cookieName" value="theme" />
<property name="defaultThemeName" value="default" />
</bean>
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="validationMessageSource" ref="messageSource" />
</bean>
<bean
class="org.springframework.web.multipart.support.StandardServletMultipartResolver"
id="multipartResolver" />
<!-- Configure to plugin JSON as request and response in method handler -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonMessageConverter" />
</list>
</property>
</bean>
<!-- Configure bean to convert JSON to POJO and vice versa -->
<bean id="jsonMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
where is the problem?
finally i figured out the problem ...
i have to set
messageSource.setUseCodeAsDefaultMessage(false);
and then all message params replaced with the correct value !

How to add google OAuth(spring-social-google) to the existing spring project using spring-social?

I developed spring application with oauth authentication using spring social project. I'm currently able to authenticate using facebook, twitter and linkedin. I would like to also add google authentication using spring-social-google. Its really confusing for me where to add google's connection factory in the existing spring social configuration.
My current spring social application context is
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:facebook="http://www.springframework.org/schema/social/facebook"
xmlns:twitter="http://www.springframework.org/schema/social/twitter"
xmlns:social="http://www.springframework.org/schema/social"
xmlns:linkedin="http://www.springframework.org/schema/social/linkedin"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/social/facebook http://www.springframework.org/schema/social/spring-social-facebook.xsd
http://www.springframework.org/schema/social/linkedin http://www.springframework.org/schema/social/spring-social-linkedin.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/social/twitter http://www.springframework.org/schema/social/spring-social-twitter.xsd
http://www.springframework.org/schema/social http://www.springframework.org/schema/social/spring-social.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:property-placeholder location="/WEB-INF/properties/application.properties" />
<facebook:config app-id="${facebook.clientId}" app-secret="${facebook.clientSecret}" app-namespace="my-app" />
<twitter:config app-id="${twitter.consumerKey}" app-secret="${twitter.consumerSecret}"/>
<linkedin:config app-id="${linkedin.consumerKey}" app-secret="${linkedin.consumerSecret}"/>
<social:jdbc-connection-repository/>
<bean id="userIdSource" class="org.springframework.social.security.AuthenticationNameUserIdSource" />
<bean id="connectController" class="org.springframework.social.connect.web.ConnectController" autowire="constructor">
<property name="connectInterceptors">
<list>
<bean class="com.prs.ony.controller.facebook.PostToWallAfterConnectInterceptor" />
<bean class="com.prs.ony.controller.twitter.TweetAfterConnectInterceptor" />
</list>
</property>
</bean>
<bean id="psc" class="org.springframework.social.connect.web.ProviderSignInController" autowire="constructor" />
<bean id="signInAdapter" class="com.prs.ony.controller.signin.SimpleSignInAdapter" autowire="constructor" />
<!-- <bean id="disconnectController" class="org.springframework.social.facebook.web.DisconnectController"
c:_0-ref="usersConnectionRepository" c:_1="${facebook.clientSecret}" /> -->
</beans>
Now im confused where to add the googles connection factory. It has to work along with the existing there oauth providers.
<bean id="connectionFactoryLocator" class="org.springframework.social.connect.support.ConnectionFactoryRegistry">
<property name="connectionFactories">
<list>
<bean class="org.springframework.social.google.connect.GoogleConnectionFactory">
<constructor-arg value="${google.consumerKey}" />
<constructor-arg value="${google.consumerSecret}" />
</bean>
</list>
</property>
</bean>
Any help would be highly appreciated.
It is possible to omit the usage of the concrete social configuration namespace and register connection factories all together:
<bean id="connectionFactoryLocator" class="org.springframework.social.connect.support.ConnectionFactoryRegistry">
<property name="connectionFactories">
<list>
<bean class="org.springframework.social.facebook.connect.FacebookConnectionFactory">
<constructor-arg name="appId" value="${facebook.clientId}" />
<constructor-arg name="appSecret" value="${facebook.clientSecret}" />
</bean>
<bean class="org.springframework.social.twitter.connect.TwitterConnectionFactory">
<constructor-arg name="consumerKey" value="${twitter.consumerKey}" />
<constructor-arg name="consumerSecret" value="${twitter.consumerSecret}" />
</bean>
<bean class="org.springframework.social.linkedin.connect.LinkedInConnectionFactory">
<constructor-arg name="consumerKey" value="${linkedin.consumerKey}" />
<constructor-arg name="consumerSecret" value="${linkedin.consumerSecret}" />
</bean>
<bean class="org.springframework.social.google.connect.GoogleConnectionFactory">
<constructor-arg name="clientId" value="${google.consumerKey}" />
<constructor-arg name="clientSecret" value="${google.consumerSecret}" />
</bean>
</list>
</property>
</bean>

PayloadValidatingInterceptor - validate only concrete wsdl

I have one Spring WS servlet with two endpoints and two wsdl files. Requests/responses are being validated with PayloadValidatingInterceptor. Content of spring-ws-servlet.xml:
<context:component-scan base-package="cz.legend.mzv.spi.ws.ei.endpoints" />
<context:component-scan base-package="cz.legend.mzv.spi.ws.de.endpoints" />
<sws:annotation-driven />
<sws:static-wsdl id="entityImport" location="classpath:/wsdl/entityImport.wsdl" />
<sws:static-wsdl id="documentEvidence"
location="classpath:/wsdl/documentEvidence.wsdl" />
<oxm:jaxb2-marshaller id="jaxb2Marshaller"
contextPath="cz.legend.mzv.spi.ws.jaxb.generated" />
<bean id="endpointAdapter" class="org.springframework.ws.server.endpoint.adapter.MarshallingMethodEndpointAdapter">
<constructor-arg ref="jaxb2Marshaller" />
</bean>
<sws:interceptors>
<bean
class="org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor">
<property name="schema" value="classpath:/xsd/vums_spi_de.xsd" />
<property name="validateRequest" value="true" />
<property name="validateResponse" value="true" />
</bean>
</sws:interceptors>
Interceptor is applied on both services. I need the interceptor to be applied only on service described by documentEvidence.wsdl. One option is to make two separate spring servlets. But I want to use only one servlet.
Solution:
Alternatively, you can use or
elements to specify for which payload root name or SOAP action the
interceptor should apply:
<sws:interceptors>
<bean class="samples.MyGlobalInterceptor"/>
<sws:payloadRoot namespaceUri="http://www.example.com">
<bean class="samples.MyPayloadRootInterceptor"/>
</sws:payloadRoot>
<sws:soapAction value="http://www.example.com/SoapAction">
<bean class="samples.MySoapActionInterceptor1"/>
<ref bean="mySoapActionInterceptor2"/>
</sws:soapAction>
</sws:interceptors>

How do I set up custom Mongo formatters in Spring?

I've been at this for a few hours and haven't found anyone that's gotten this working yet. I want to persist a BigDecimal object in Mongo, but Mongo doesn't natively support BigDecimal. I followed Spring's docs here but no luck.
From what I can tell Spring isn't injecting my custom converter classes into Mongo when it's writing to the db. Here's what I have done:
My applicationContext-services.xml
...
<!-- Factory bean that creates the Mongo instance -->
<mongo:mongo
host="localhost"
port="1234" />
<mongo:db-factory
dbname="solar"
mongo-ref="mongo"/>
<mongo:mapping-converter>
<mongo:custom-converters>
<mongo:converter>
<bean class="com.mine.BigDecimalReadConverter"/>
</mongo:converter>
<mongo:converter>
<bean class="com.mine..BigDecimalWriteConverter"/>
</mongo:converter>
</mongo:custom-converters>
</mongo:mapping-converter>
<!-- Use this post processor to translate any MongoExceptions thrown in #Repository annotated classes -->
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<bean id="mongoDbTest"
class="com.mine.MongoDbTest">
<property name="mongoTemplate">
<ref local="mongoTemplate" />
</property>
</bean>
The error I'm getting is:
java.lang.IllegalArgumentException: Multiple constructors with arguments found in class java.math.BigDecimal! Annotate one with #PreferedConstructor explicitly to select it to be used in persistence operations.
at org.springframework.data.mapping.PreferredConstructorDiscoverer.<init>(PreferredConstructorDiscoverer.java:81)
Try using this for converter support:
<bean id="mappingContext"
class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" lazy-init="true"/>
<bean id="defaultMongoTypeMapper"
class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper" lazy-init="true">
<constructor-arg name="typeKey"><null/></constructor-arg>
</bean>
<bean id="mappingMongoConverter"
class="org.springframework.data.mongodb.core.convert.MappingMongoConverter" lazy-init="true" >
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
<constructor-arg name="mappingContext" ref="mappingContext" />
<property name="typeMapper" ref="defaultMongoTypeMapper" />
</bean>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate" lazy-init="true">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
<constructor-arg name="mongoConverter" ref="mappingMongoConverter" />
</bean>
Here is how I solved this. The order in which you define the beans matters. So my app.xml that I got it working with is:
<bean id="mappingContext" class="org.springframework.data.document.mongodb.mapping.MongoMappingContext"/>
<bean id="readConverter" class="com.mine.BigDecimalReadConverter"/>
<bean id="writeConverter" class="com.mine.BigDecimalWriteConverter"/>
<mongo:mapping-converter id="mappingConverter">
<mongo:custom-converters>
<mongo:converter ref="readConverter" />
<mongo:converter ref="writeConverter" />
</mongo:custom-converters>
</mongo:mapping-converter>
<!-- Factory bean that creates the Mongo instance -->
<mongo:mongo
host="${${environment}.mongodb.host}"
port="${${environment}.mongodb.port}" />
<mongo:db-factory
dbname="${${environment}.mongodb.databaseName}"
mongo-ref="mongo"/>
<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
<constructor-arg name="mongoConverter" ref="mappingConverter"/>
</bean>
<!-- Use this post processor to translate any MongoExceptions thrown in #Repository annotated classes -->
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

Resources