cxf-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nagulapalli, Srinivas" <Srinivas.Nagulapa...@ncr.com>
Subject Help- Client invocation with PathParam does not call ccustom ParameterConverter for JAX_RS service using cxf-3.0.2-SNAPSHOT
Date Thu, 18 Sep 2014 10:15:23 GMT
Hi
When calling a service using PathParam with an object containing java.util.Date, the custom
ParamConverter is NOT getting invoked.
That is, getUserByDateRange2(GetUserByDateRangeRequest2 request) does NOT call SomeDateConverter
in code below.
Instead gives following error on Server.log;
--------------------------------------------------------------------
Payload: Parameter Class com.my.demo.rest.services.GetUserByDateRangeRequest2 has no constructor
with single String parameter, static valueOf(String) or fromString(String) methods
--------------------------------------------------------------------

However, it is correctly getting invoked when using MatrixParam. That is, getUserByDateRange(GetUserByDateRange
request) calls SomeDateConverter as expected.

Code tested using Apache CXF 3.0.2-SNAPSHOT (because it has fix for the needed Date marshalling)

Am I missing something?

Appreciate greatly any help/pointers.

Thanks
Srini

//////////////////////////////////////////////////////////////////////////

/////Client Side:
public class UserManagerServiceTest {
        public static void main(String[] args) {
                ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext
(new String[]{"rest-client.xml"});
                UserManager userManagerService = (UserManager) appContext.getBean("userManagerService");

                Date startDate = new DateTime(1960, 1, 1, 0, 0).toDate();
                Date endDate = new DateTime(1982, 1, 1, 0, 0).toDate();

                GetUserByDateRangeRequest request = new GetUserByDateRangeRequest(startDate,
endDate);
                String s2 = userManagerService.getUserByDateRange3(request); //This invokes
SomeDateConverter- WORKS:-)

                GetUserByDateRangeRequest2 request2 = new GetUserByDateRangeRequest2(startDate);
                String pathParamResult = userManagerService.getUserByDateRange2(request2);
//This does NOT invoke SomeDateConverter!!!

                appContext.close();

        }
}
//////////////////////////////////////////////
///rest-client.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:cxf="http://cxf.apache.org/core"
        xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs-client"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                http://cxf.apache.org/jaxrs-client http://cxf.apache.org/schemas/jaxrs-client.xsd
        http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
        http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">


        <bean id="someDateConverter" class="com.my.demo.util.SomeDateConverter" />
        <bean id="paramConverterProvider" class="com.my.demo.util.ParamConverterProviderImpl">
                <constructor-arg>
                        <map>
                                <entry key="java.util.Date" value-ref="someDateConverter"
/>
                        </map>
                </constructor-arg>
        </bean>

        <jaxrs:client id="userManagerService"
                address="http://localhost:8080/RestDemo2/services/rest/UserManager"
                serviceClass="com.my.demo.rest.services.UserManager"
                inheritHeaders="true">
                <jaxrs:providers>
                        <ref bean="paramConverterProvider" />
                </jaxrs:providers>
        </jaxrs:client>

</beans>
//////////////////////////////////////////////////////////////////////

//////////////Server side code below
//UserManager.java
package com.my.demo.rest.services;

import java.util.Date;

import javax.jws.WebParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.MatrixParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import com.my.demo.rest.model.UserRequest;
import com.my.demo.rest.model.UserResponse;

@Consumes("application/json")
@Produces("application/json")
public interface UserManager
{
         @GET
         @Path("/getUserByDateRange")
         @Produces(MediaType.TEXT_PLAIN)
        public String getUserByDateRange(@MatrixParam("") @WebParam(name = "request") GetUserByDateRangeRequest
request);


         @GET
         @Path("/getUserByDateRange2/{request}")
         @Produces(MediaType.TEXT_PLAIN)
        public String getUserByDateRange2(@PathParam("request")  @WebParam(name = "request")GetUserByDateRangeRequest2
request);


         @GET
         @Path("/getUserByDateRange3")
         @Produces(MediaType.TEXT_PLAIN)
        public String getUserByDateRange3(@QueryParam("") @WebParam(name = "request") GetUserByDateRangeRequest
request);

}
//======================================================================================
//UserManagerService.java
package com.my.demo.rest.services.impl;

import java.util.Date;
import java.util.List;

import javax.xml.ws.Endpoint;

import com.my.demo.rest.dao.UserManagerDao;
import com.my.demo.rest.model.User;
import com.my.demo.rest.model.UserRequest;
import com.my.demo.rest.model.UserResponse;
import com.my.demo.rest.services.GetUserByDateRangeRequest;
import com.my.demo.rest.services.GetUserByDateRangeRequest2;
import com.my.demo.rest.services.UserManager;

public class UserManagerService implements UserManager
{
        private UserManagerDao userDao;

        public UserManagerDao getUserDao()
        {
                return userDao;
        }

        public void setUserDao(UserManagerDao userDao)
        {
                this.userDao = userDao;
        }

        public String getUserByDateRange(GetUserByDateRangeRequest request)
        {
                String response = new String();

                StringBuffer buf = new StringBuffer();
                Date startDate = request.getStartDate();
                Date endDate = request.getEndDate();
                try
                {
                        List<User> listUsers = getUserDao().fetchUserByDateRange(startDate,
endDate);
                        for (User user: listUsers){
                                buf.append("User:" + user);
                                buf.append("\n");
                        }
                        return buf.toString();
                }
                catch (Exception e)
                {
                        response = "Error:"+ e.getClass() + ": " + e.getMessage();
                }

                return response;
        }

        public String getUserByDateRange3(GetUserByDateRangeRequest request)
        {
                String response = new String();

                StringBuffer buf = new StringBuffer();
                Date startDate = request.getStartDate();
                Date endDate = request.getEndDate();
                try
                {
                        List<User> listUsers = getUserDao().fetchUserByDateRange(startDate,
endDate);
                        for (User user: listUsers){
                                buf.append("User:" + user);
                                buf.append("\n");
                        }
                        return buf.toString();
                }
                catch (Exception e)
                {
                        response = "Error:"+ e.getClass() + ": " + e.getMessage();
                }

                return response;
        }


        public String getUserByDateRange2(GetUserByDateRangeRequest2 request)
        {
                String response = new String();

                StringBuffer buf = new StringBuffer();
                Date startDate = request.getStartDate();
                Date endDate = new Date();
                try
                {
                        List<User> listUsers = getUserDao().fetchUserByDateRange(startDate,
endDate);
                        for (User user: listUsers){
                                buf.append("User:" + user);
                                buf.append("\n");
                        }
                        return buf.toString();
                }
                catch (Exception e)
                {
                        response = "Error:"+ e.getClass() + ": " + e.getMessage();
                }

                return response;
        }
}
///////////////////////////////////////
//SomeDateConverter.javapackage com.my.demo.util;

import java.util.Date;

import javax.ws.rs.ext.ParamConverter;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.ISODateTimeFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SomeDateConverter implements ParamConverter<Date> {

        Logger logger = LoggerFactory.getLogger(SomeDateConverter.class);
        @Override
        public Date fromString(String dateString) {
                logger.info("***Entered fromString() - dateString=" + dateString);

                if (dateString.contains("-")) {
                        DateTime result = ISODateTimeFormat.dateTimeParser()
                                        .withZone(DateTimeZone.UTC).parseDateTime(dateString);
                        return result.toDate();
                }
                return new Date(Long.parseLong(dateString));
        }

        @Override
        public String toString(Date dateToConvert) {
                logger.info("***Entered SomeDateConverter - toString() dateToConvert=" + dateToConvert);
                return ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC)
                                .print(dateToConvert.getTime());
        }

        public static void main(String[] args) {
                SomeDateConverter sdc = new SomeDateConverter();

                String s = "1960-01-01T05:00:00.000Z";
                /*String s = "Thu Dec 31 19:00:00 EST 1959"; */
                Date d = sdc.fromString(s);
                System.out.println("For s=" + s + "; date converted is:" + d);


                //Date now = new Date();
                //System.out.println("Date(now)=" + now + "; in ISO format toDate()=" + sdc.toString(now));

        }
}
///////////////
//ParamConverterProviderImpl.java
package com.my.demo.util;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Map;
import javax.ws.rs.ext.ParamConverter;
import javax.ws.rs.ext.ParamConverterProvider;

public class ParamConverterProviderImpl implements ParamConverterProvider {
        private final Map<Class<?>, ? extends ParamConverter<?>> paramConverters;

        public ParamConverterProviderImpl(
                        Map<Class<?>, ? extends ParamConverter<?>> paramConverters)
{
                this.paramConverters = paramConverters;
        }

        @Override
        public <T> ParamConverter<T> getConverter(Class<T> rawClass,
                        Type genericType, Annotation[] annotations) {
                return (ParamConverter<T>) paramConverters.get(rawClass);
        }
}
///////////////////////
//rest-context.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:cxf="http://cxf.apache.org/core" xmlns:jaxws="http://cxf.apache.org/jaxws"
        xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation=" http://cxf.apache.org/core
        http://cxf.apache.org/schemas/core.xsd 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 http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
-->
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://cxf.apache.org/core"
        xmlns:jaxws="http://cxf.apache.org/jaxws" 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-client http://cxf.apache.org/schemas/jaxrs-client.xsd
        http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
        http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">

        <import resource="classpath:META-INF/cxf/cxf.xml" />
        <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

        <cxf:bus>
                <cxf:features>
                        <cxf:logging />
                </cxf:features>
        </cxf:bus>

        <bean id="someDateConverter" class="com.my.demo.util.SomeDateConverter" />
        <bean id="paramConverterProvider" class="com.my.demo.util.ParamConverterProviderImpl">
                <constructor-arg>
                        <map>
                                <entry key="java.util.Date" value-ref="someDateConverter"
/>
                        </map>
                </constructor-arg>
        </bean>

        <bean id="userDao"
                class="com.my.demo.rest.dao.impl.UserManagerMemoryDao">
        </bean>

        <bean id="userManagerService"
                class="com.my.demo.rest.services.impl.UserManagerService">
                <property name="userDao" ref="userDao" />
        </bean>

        <bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"
/>

        <jaxrs:server id="userManagerREST" address="/rest/UserManager">
                <jaxrs:serviceBeans>
                        <ref bean="userManagerService" />
                </jaxrs:serviceBeans>
                <jaxrs:providers>
                        <ref bean='jsonProvider' />
                        <ref bean="paramConverterProvider" />
                </jaxrs:providers>
        </jaxrs:server>


</beans>
////////////////////////////


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message