cxf rest multiple endpoints - spring

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>

Related

Update one column does not works

I want to update one column of my database. The column name is status and the type is Enum('waiting' , 'accepted' , 'rejected')
I want to update this column when I click on link:
Accept
Reject
Vacation.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.terafast.tem.model">
<class name="Vacation" table="vacations" dynamic-update="true">
<id name="id" column="REQUEST_ID">
<generator class="native" />
</id>
<property name="user" column="USER_ID" />
<property name="reason" column="REASON" />
<property name="duration" column="HOW_LONG" />
<property name="status" column="STATUS" />
<property name="start" type="date" column="START_DATE" />
<property name="created" type="date" column="CREATED_AT" />
</class>
</hibernate-mapping>
Controller
#RequestMapping(value = "/requests/action")
public String statuHandler(HttpServletRequest request, Model model) {
int id = Integer.parseInt(request.getParameter("id"));
String status = request.getParameter("a");
vacationDao.actionStatus(id, status);
return "redirect:/admin/requests";
I can successfully get these two GET values. (id , a). My VacationDAOImpl:
#Override
#Transactional
public void actionStatus(int id, String action) {
Session session = sessionFactory.openSession();
Query q = session.createQuery("from Vacation where id = :reqid ");
q.setParameter("reqid", id);
Vacation vacation = (Vacation) q.list().get(0);
vacation.setStatus(action);
session.update(vacation);
}
servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<mvc:annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources
in the /WEB-INF/views directory -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<context:component-scan base-package="com.terafast.tem" />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="employeeDao" class="com.terafast.tem.dao.EmployeeDAOImpl">
<constructor-arg>
<ref bean="sessionFactory" />
</constructor-arg>
</bean>
<bean id="vacationDoa" class="com.terafast.tem.dao.VacationDAOImpl">
<constructor-arg>
<ref bean="sessionFactory" />
</constructor-arg>
</bean>
</beans>
I have seen this approach in this tutorial.
When I click on links, everything looks good. But the column value does not change. Could someone explain this problem?

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 configure multiple webservices using applicationContext.xml and CXF

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 ?

Spring 3 with Thymeleaf config issue

Hello I have the following jars in my build path -
spring-beans-3.1.2.RELEASE.jar
spring-context-3.1.2.RELEASE.jar
spring-core-3.1.2.RELEASE.jar
spring-expression-3.1.2.RELEASE.jar
spring-web-3.1.2.RELEASE.jar
spring-webmvc-3.1.2.RELEASE.jar
thymeleaf-spring3-2.0.13.jar
and my servlet
<?xml version="1.0" encoding="UTF-8"?><br>
<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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<context:component-scan base-package="web.controller" />
<!-- Enabling Spring MVC configuration through annotations -->
<mvc:annotation-driven />
<!-- Mapping Static Resources -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="order" value="1" />
<property name="viewNames" value="*.html" />
</bean>
</beans>
The error I get on launching is -
Cannot find class [org.thymeleaf.templateresolver.ServletContextTemplateResolver] for bean with name 'templateResolver' defined in ServletContext resource [/WEB-INF/springMVC-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.thymeleaf.templateresolver.ServletContextTemplateResolver
Am I missing any other library here? Any help is much appreciated.
You are missing the actual Thymeleaf jar. You included the Spring jar that provides the integration but you missed the actual implementation of it.
Download the jar from here
thymeleaf download site

spring multiple property file

I have a spring project. There are two property files. one property file is config in dbConfig.xml and I cannot change it. I have my own appConfig.xml
I have the following in it
<util:properties id="configProps" location="classpath:spring/config.properties" />
<bean id="createDummyDataTask" class="com.merc.spring.CreateDummyData" scope="step">
<property name="srcFolder" value="${configProps.srcDir}"/>
</bean>
using either srcDir or configProps.srcDir does not seem to work.
${} works only for property files loaded by the context:propertyplaceholder. If you are using spring 3.0 you can use #{} which is processed as a Spel (spring expression language). The following should work.
<util:properties id="configProps" location="classpath:spring/config.properties" />
<bean id="createDummyDataTask" class="com.merc.spring.CreateDummyData" scope="step">
<property name="srcFolder" value="#{configProps.srcDir}"/>
</bean>
Here how we resolved it
Context.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<import resource="Settings.xml"/>
<import resource="Database.xml"/>
</beans>
Settings.xml
In the list many property file could be added
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>db.properties</value>
</list>
</property>
</bean>
</beans>
Sample bean for processing db.properties file
Database.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- the transaction manager -->
<bean id="dstxnManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="appDataSource"/>
</bean>
<!-- the DataSource (parameterized for configuration via a PropertyPlaceHolderConfigurer) -->
<bean id="appDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" lazy-init="false">
<property name="driverClass" value="${driverClass}" />
<property name="jdbcUrl" value="${jdbcUrl}" />
<property name="user" value="${username}" />
<property name="password" value="${password}" />
</bean>
<!-- DB connection factory -->
<bean id="storageDaoFactory" class="com.dao.StorageDAOFactory">
<constructor-arg><ref bean="dstxnManager"/></constructor-arg>
</bean>
</beans>

Resources