cxf-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sergey Beryozkin <sberyoz...@gmail.com>
Subject Re: 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:36:33 GMT
Hi
On 18/09/14 11:30, Nagulapalli, Srinivas wrote:
>
> Hi
> Yes, the converter is registered on server too- in "rest-context.xml" (way bottom) below.
On client side "rest-client.xml"  used to register the converter.
>
Right, may be a bit of extra code is pasted here :-), not easy to see 
the relevant bits...

You have GetUserByDateRangeRequest2 but expect a Date converter be 
called so of course that won't work.

Cheers, Sergey

> Appreciate help greatly.
>
> Thanks
> Srini
>
> -----Original Message-----
> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
> Sent: Thursday, September 18, 2014 6:24 AM
> To: users@cxf.apache.org
> Cc: Nagulapalli, Srinivas
> Subject: Re: Help- Client invocation with PathParam does not call ccustom ParameterConverter
for JAX_RS service using cxf-3.0.2-SNAPSHOT
>
> Hi
>
> Please send the queries to the users list instead,
>
> Comments below,
> On 18/09/14 11:15, Nagulapalli, Srinivas wrote:
>> 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
>> --------------------------------------------------------------------
>>
> This is a server side error message, not the client side one.
> Do you have the converter registered on the server too ?
>
> Cheers, Sergey
>
>
>> 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>
>> ////////////////////////////
>


-- 
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com

Mime
View raw message