Hi

You def do not need to send the code all the time, most of this code is not relevant.

What happens is that in some cases you rely on the CXF extension where you have something like

@MatrixParam("") MyBean

In this case MyBean properties represent individual Matrix parameters and such properties may be converted with the help of the converters. Note in this case the runtime itself deals with the instantiation of MyBean and injecting matrix parameters into it.


When you have

@PathParam MyBean

it means you expect a single path parameter value converted into MyBean as opposed to to one of MyBean properties.

Perhaps, in your case, you may want to do @BeanParam MyBean and have the individual MyBean property annotated with @PathParam, etc

Sergey


On 18/09/14 14:09, Nagulapalli, Srinivas wrote:
[Resending attaching with code this time for easy reference- Thanks]

-----Original Message-----
From: Sergey Beryozkin [mailto:[email protected]]

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.

Please tell, what should be done to make Date converter to be called when using 
PathParam passing an object containing a java.util.Date object.

Of course, when using MatrixParam, passing GetUserByDateRangeRequest object, 
custom ParamConverter SomeDateConverter is correctly getting invoked.

Thanks much again

Regards
Srini

==============
//UserManagerServiceTest.java
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();             
        }
}
////////////////////////////////
//UserManager.java

@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);   
        
        @GET
        @Path("/getUserBeforeDate/{startDate}")
         @Produces(MediaType.TEXT_PLAIN)
        public String getUserBeforeDate(@PathParam("startDate") Date startDate);
        
}
/////////////////////
//config used on client-side "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:headers>
                        <entry key="Accept" value="application/json" />
                        <entry key="Content-Type" value="application/json" />
                </jaxrs:headers> -->
                <jaxrs:providers>
                        <ref bean="paramConverterProvider" />
                </jaxrs:providers>
        </jaxrs:client>

</beans>
//////////////
//SomeDateConverter.java
public class SomeDateConverter implements ParamConverter<Date> {  
        @Override
        public Date fromString(String 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) {
                return ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC)  
.print(dateToConvert.getTime());
        }
}
//ParamConverterProviderImpl.java
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);
        }
}
//////


Reply via email to