You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Raphael Jolivet <ra...@gmail.com> on 2013/04/17 10:15:42 UTC

Serialize parameters on client side

Hi,

I am developping a REST application with Apache CXF.

On server side, I have setup a ParameterHandler in order to parse Dates with
a custom format.
What is the counterpart on client side in order to serialize a parameter ?

Thanks
Raphael




--
View this message in context: http://cxf.547215.n5.nabble.com/Serialize-parameters-on-client-side-tp5726421.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Serialize parameters on client side

Posted by Sergey Beryozkin <sb...@gmail.com>.
I guess I know what it is, assuming it CXF 2.7.x, JAX-RS 2.0 param 
converters are complex, you actually need to register 
ParamConverterProvider

http://jax-rs-spec.java.net/nonav/2.0/apidocs/javax/ws/rs/ext/ParamConverterProvider.html

it will return specific ParamConverters,

example, you may have

@Provider
class ParamConverterProvider implements ParamConverterProvider {

   public ParamConverter getConverter(...) {

        return new TimestampParamConverter();
   }

}

HTH, Sergey

On 04/09/13 15:42, Sergey Beryozkin wrote:
> Hi,
>
> What CXF version and how method Date parameter is annotated ?
>
> Cheers, Sergey
> On 04/09/13 15:24, eanbiso wrote:
>> Hi Sergey,
>> I'm in a similar situation...I must use a parameter converter for Date
>> type
>> to be sure at the serialization of a Date parameter the toString() method
>> adds also the milliseconds info in the produced string (until now I've
>> seen
>> that when a Date obj is serialized by default the string is in the format
>> "Wed Sep 04 16:09:02 CEST 2013" and when I rebuild the Date obj at server
>> side I lose the ms info).
>> So I've built a new parameter converter for type Date like this:
>>
>> @Provider
>> public class TimestampParamConverter implements ParamConverter<Date> {
>>
>>     /**
>>      * Required for REST ws invocation
>>      */
>>     @Override
>>     public Date fromString(String s) {
>>         //EXAMPLE TEST
>>         //example s: Mon May 13 14:21:47 CEST 2013
>>         SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd
>> HH:mm:ss
>> zzz yyyy", Locale.US);
>>         try {
>>             return new Date(dateFormat.parse(s).getTime());
>>         } catch (ParseException e) {
>>             log.error("Exception building the Date from corresponding
>> string at REST
>> ws invocation. We return a NULL object. ",e);
>>             return null;
>>         }
>>     }
>>
>>     @Override
>>     public String toString(Date arg0) throws IllegalArgumentException {
>>         // EXAMPLE TEST
>>         if(arg0 != null){
>>             return arg0.toString();
>>         }else{
>>             return null;
>>         }
>>     }
>> }
>>
>> and I've added it on client side loading the endPoint in this way:
>>
>>     JAXRSClientFactoryBean proxyFactory = new JAXRSClientFactoryBean();
>>     proxyFactory.setServiceClass(clazz);
>>     proxyFactory.setAddress(address);
>>     List providers = new ArrayList();
>>     providers.add(new StringArrayBodyReader());
>>     providers.add(new TimestampParamConverter());
>>     proxyFactory.setProviders(providers);
>>     .....
>>
>> However I'm not able to arrive in the toString() method at the REST API
>> invocation....
>> What am I doing wrong?
>> Thanks a lot,
>>
>> Andrea
>>
>>
>>
>> --
>> View this message in context:
>> http://cxf.547215.n5.nabble.com/Serialize-parameters-on-client-side-tp5726421p5733516.html
>>
>> Sent from the cxf-user mailing list archive at Nabble.com.
>>
>
>


-- 
Sergey Beryozkin

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

Blog: http://sberyozkin.blogspot.com

Re: Serialize parameters on client side

Posted by arjun dhar <dh...@yahoo.com>.
Oh Darn! I found the problem. 
its "ParamConverterProvider" not "ParameterConverterProvider"

Sorry



--
View this message in context: http://cxf.547215.n5.nabble.com/Serialize-parameters-on-client-side-tp5726421p5777938.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Serialize parameters on client side

Posted by arjun dhar <dh...@yahoo.com>.
Hi,
Im upgrading from 2.7.5 to 3.1.10. I cant find the maven artifact that
contains "ParameterConverterProvider". Am sorry its a bit silly but its
driving me nuts.

Based on the doc http://cxf.apache.org/docs/jax-rs.html --
https://jax-rs-spec.java.net/nonav/2.0/apidocs/javax/ws/rs/ext/ParamConverterProvider.html;
I assumed it would be in 

.. but was not!

please help,
thanks



--
View this message in context: http://cxf.547215.n5.nabble.com/Serialize-parameters-on-client-side-tp5726421p5777937.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Serialize parameters on client side

Posted by Sergey Beryozkin <sb...@gmail.com>.
Right, yes, please check the email I've just sent, you need to register 
a ParamConverterProvider :-)

FYI, in CXF 3.0.0 CXF ParameterHandlers have gone, they are simpler to 
use but with 2.0 now becoming the main spec CXF specific extensions 
duplicating one way or the other 2.0 features had to go...

Cheers, Sergey
On 04/09/13 15:53, eanbiso wrote:
> Hi,
> I'm using cxf2.7.3 and I have some methods like for example:
>
>      @GET
>      @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
>      @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
>      @Path("/GetPacketByArrivalTimeAndConfigIdWithOffset")
>      @ApiOperation(value = " Gets the list of IProxyPacket objects identified by the given information.",
>                      notes="Returns the list of objects identified by the given input", response = IProxyPacket.class, produces ="application/xml,application/json")
>      public List<IProxyPacket> GetPacketByArrivalTimeAndConfigIdWithOffset(
>              @ApiParam(value = "appGardenID", required = true) @WebParam(name = "appGardenID") @QueryParam("appGardenID") int appGardenID,
>              @ApiParam(value = "configId", required = true) @WebParam(name = "configId") @QueryParam("configId") @CxfWSAuthGrain(type=authType.config) int configId,
>              @ApiParam(value = "lowerBound", required = true) @WebParam(name = "lowerBound") @QueryParam("lowerBound") Date lowerBound,
>              @ApiParam(value = "upperBound", required = true) @WebParam(name = "upperBound") @QueryParam("upperBound") Date upperBound,
>              @ApiParam(value = "maxResult", required = true) @WebParam(name = "maxResult") @QueryParam("maxResult") Integer maxResult,
>              @ApiParam(value = "offset", required = true) @WebParam(name = "offset") @QueryParam("offset") Integer offset);
>
> On server side until now I'm using a
>     TimestampParameterHandler implements ParameterHandler<Date>{...}
> that is properly recalled,
> while on client side I'm not able to enter in the toString of the new parameter converter.
>
>
> Andrea
>
> Date: Wed, 4 Sep 2013 07:43:27 -0700
> From: ml-node+s547215n5733518h78@n5.nabble.com
> To: bisomagic@hotmail.it
> Subject: Re: Serialize parameters on client side
>
>
>
> 	Hi,
>
>
> What CXF version and how method Date parameter is annotated ?
>
>
> Cheers, Sergey
>
> On 04/09/13 15:24, eanbiso wrote:
>
>> Hi Sergey,
>
>> I'm in a similar situation...I must use a parameter converter for Date type
>
>> to be sure at the serialization of a Date parameter the toString() method
>
>> adds also the milliseconds info in the produced string (until now I've seen
>
>> that when a Date obj is serialized by default the string is in the format
>
>> "Wed Sep 04 16:09:02 CEST 2013" and when I rebuild the Date obj at server
>
>> side I lose the ms info).
>
>> So I've built a new parameter converter for type Date like this:
>
>>
>
>> @Provider
>
>> public class TimestampParamConverter implements ParamConverter<Date> {
>
>> 	
>
>> 	/**
>
>> 	 * Required for REST ws invocation
>
>> 	 */
>
>> 	@Override
>
>> 	public Date fromString(String s) {
>
>> 		//EXAMPLE TEST
>
>> 		//example s: Mon May 13 14:21:47 CEST 2013
>
>> 		SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss
>
>> zzz yyyy", Locale.US);
>
>> 		try {
>
>> 			return new Date(dateFormat.parse(s).getTime());
>
>> 		} catch (ParseException e) {
>
>> 			log.error("Exception building the Date from corresponding string at REST
>
>> ws invocation. We return a NULL object. ",e);
>
>> 			return null;
>
>> 		}
>
>> 	}
>
>>
>
>> 	@Override
>
>> 	public String toString(Date arg0) throws IllegalArgumentException {
>
>> 		// EXAMPLE TEST
>
>> 		if(arg0 != null){
>
>> 			return arg0.toString();
>
>> 		}else{
>
>> 			return null;
>
>> 		}
>
>> 	}
>
>> }
>
>>
>
>> and I've added it on client side loading the endPoint in this way:
>
>>
>
>>      JAXRSClientFactoryBean proxyFactory = new JAXRSClientFactoryBean();
>
>>      proxyFactory.setServiceClass(clazz);
>
>>      proxyFactory.setAddress(address);
>
>>      List providers = new ArrayList();
>
>>      providers.add(new StringArrayBodyReader());
>
>>      providers.add(new TimestampParamConverter());
>
>>      proxyFactory.setProviders(providers);
>
>>      .....
>
>>
>
>> However I'm not able to arrive in the toString() method at the REST API
>
>> invocation....
>
>> What am I doing wrong?
>
>> Thanks a lot,
>
>>
>
>> Andrea
>
>>
>
>>
>
>>
>
>> --
>
>> View this message in context: http://cxf.547215.n5.nabble.com/Serialize-parameters-on-client-side-tp5726421p5733516.html
>> Sent from the cxf-user mailing list archive at Nabble.com.
>
>>
>
>
>
>
> 	
> 	
> 	
> 	
>
> 	
>
> 	
> 	
> 		If you reply to this email, your message will be added to the discussion below:
> 		http://cxf.547215.n5.nabble.com/Serialize-parameters-on-client-side-tp5726421p5733518.html
> 	
> 	
> 		
> 		To unsubscribe from Serialize parameters on client side, click here.
>
> 		NAML
> 	 		 	   		
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Serialize-parameters-on-client-side-tp5726421p5733519.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>


-- 
Sergey Beryozkin

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

Blog: http://sberyozkin.blogspot.com

RE: Serialize parameters on client side

Posted by eanbiso <bi...@hotmail.it>.
Hi,
I'm using cxf2.7.3 and I have some methods like for example:

    @GET
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    @Path("/GetPacketByArrivalTimeAndConfigIdWithOffset")
    @ApiOperation(value = " Gets the list of IProxyPacket objects identified by the given information.", 
                    notes="Returns the list of objects identified by the given input", response = IProxyPacket.class, produces ="application/xml,application/json")
    public List<IProxyPacket> GetPacketByArrivalTimeAndConfigIdWithOffset(
            @ApiParam(value = "appGardenID", required = true) @WebParam(name = "appGardenID") @QueryParam("appGardenID") int appGardenID, 
            @ApiParam(value = "configId", required = true) @WebParam(name = "configId") @QueryParam("configId") @CxfWSAuthGrain(type=authType.config) int configId, 
            @ApiParam(value = "lowerBound", required = true) @WebParam(name = "lowerBound") @QueryParam("lowerBound") Date lowerBound,  
            @ApiParam(value = "upperBound", required = true) @WebParam(name = "upperBound") @QueryParam("upperBound") Date upperBound,  
            @ApiParam(value = "maxResult", required = true) @WebParam(name = "maxResult") @QueryParam("maxResult") Integer maxResult, 
            @ApiParam(value = "offset", required = true) @WebParam(name = "offset") @QueryParam("offset") Integer offset);

On server side until now I'm using a 
   TimestampParameterHandler implements ParameterHandler<Date>{...}
that is properly recalled,
while on client side I'm not able to enter in the toString of the new parameter converter.


Andrea

Date: Wed, 4 Sep 2013 07:43:27 -0700
From: ml-node+s547215n5733518h78@n5.nabble.com
To: bisomagic@hotmail.it
Subject: Re: Serialize parameters on client side



	Hi,


What CXF version and how method Date parameter is annotated ?


Cheers, Sergey

On 04/09/13 15:24, eanbiso wrote:

> Hi Sergey,

> I'm in a similar situation...I must use a parameter converter for Date type

> to be sure at the serialization of a Date parameter the toString() method

> adds also the milliseconds info in the produced string (until now I've seen

> that when a Date obj is serialized by default the string is in the format

> "Wed Sep 04 16:09:02 CEST 2013" and when I rebuild the Date obj at server

> side I lose the ms info).

> So I've built a new parameter converter for type Date like this:

>

> @Provider

> public class TimestampParamConverter implements ParamConverter<Date> {

> 	

> 	/**

> 	 * Required for REST ws invocation

> 	 */

> 	@Override

> 	public Date fromString(String s) {

> 		//EXAMPLE TEST

> 		//example s: Mon May 13 14:21:47 CEST 2013

> 		SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss

> zzz yyyy", Locale.US);

> 		try {

> 			return new Date(dateFormat.parse(s).getTime());

> 		} catch (ParseException e) {

> 			log.error("Exception building the Date from corresponding string at REST

> ws invocation. We return a NULL object. ",e);

> 			return null;

> 		}

> 	}

>

> 	@Override

> 	public String toString(Date arg0) throws IllegalArgumentException {

> 		// EXAMPLE TEST

> 		if(arg0 != null){

> 			return arg0.toString();

> 		}else{

> 			return null;

> 		}

> 	}

> }

>

> and I've added it on client side loading the endPoint in this way:

>

>     JAXRSClientFactoryBean proxyFactory = new JAXRSClientFactoryBean();

>     proxyFactory.setServiceClass(clazz);

>     proxyFactory.setAddress(address);

>     List providers = new ArrayList();

>     providers.add(new StringArrayBodyReader());

>     providers.add(new TimestampParamConverter());

>     proxyFactory.setProviders(providers);

>     .....

>

> However I'm not able to arrive in the toString() method at the REST API

> invocation....

> What am I doing wrong?

> Thanks a lot,

>

> Andrea

>

>

>

> --

> View this message in context: http://cxf.547215.n5.nabble.com/Serialize-parameters-on-client-side-tp5726421p5733516.html
> Sent from the cxf-user mailing list archive at Nabble.com.

>




	
	
	
	

	

	
	
		If you reply to this email, your message will be added to the discussion below:
		http://cxf.547215.n5.nabble.com/Serialize-parameters-on-client-side-tp5726421p5733518.html
	
	
		
		To unsubscribe from Serialize parameters on client side, click here.

		NAML
	 		 	   		  



--
View this message in context: http://cxf.547215.n5.nabble.com/Serialize-parameters-on-client-side-tp5726421p5733519.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Serialize parameters on client side

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi,

What CXF version and how method Date parameter is annotated ?

Cheers, Sergey
On 04/09/13 15:24, eanbiso wrote:
> Hi Sergey,
> I'm in a similar situation...I must use a parameter converter for Date type
> to be sure at the serialization of a Date parameter the toString() method
> adds also the milliseconds info in the produced string (until now I've seen
> that when a Date obj is serialized by default the string is in the format
> "Wed Sep 04 16:09:02 CEST 2013" and when I rebuild the Date obj at server
> side I lose the ms info).
> So I've built a new parameter converter for type Date like this:
>
> @Provider
> public class TimestampParamConverter implements ParamConverter<Date> {
> 	
> 	/**
> 	 * Required for REST ws invocation
> 	 */
> 	@Override
> 	public Date fromString(String s) {
> 		//EXAMPLE TEST
> 		//example s: Mon May 13 14:21:47 CEST 2013
> 		SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss
> zzz yyyy", Locale.US);
> 		try {
> 			return new Date(dateFormat.parse(s).getTime());
> 		} catch (ParseException e) {
> 			log.error("Exception building the Date from corresponding string at REST
> ws invocation. We return a NULL object. ",e);
> 			return null;
> 		}
> 	}
>
> 	@Override
> 	public String toString(Date arg0) throws IllegalArgumentException {
> 		// EXAMPLE TEST
> 		if(arg0 != null){
> 			return arg0.toString();
> 		}else{
> 			return null;
> 		}
> 	}
> }
>
> and I've added it on client side loading the endPoint in this way:
>
>     JAXRSClientFactoryBean proxyFactory = new JAXRSClientFactoryBean();
>     proxyFactory.setServiceClass(clazz);
>     proxyFactory.setAddress(address);
>     List providers = new ArrayList();
>     providers.add(new StringArrayBodyReader());
>     providers.add(new TimestampParamConverter());
>     proxyFactory.setProviders(providers);
>     .....
>
> However I'm not able to arrive in the toString() method at the REST API
> invocation....
> What am I doing wrong?
> Thanks a lot,
>
> Andrea
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Serialize-parameters-on-client-side-tp5726421p5733516.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>



Re: Serialize parameters on client side

Posted by eanbiso <bi...@hotmail.it>.
Hi Sergey,
I'm in a similar situation...I must use a parameter converter for Date type
to be sure at the serialization of a Date parameter the toString() method
adds also the milliseconds info in the produced string (until now I've seen
that when a Date obj is serialized by default the string is in the format
"Wed Sep 04 16:09:02 CEST 2013" and when I rebuild the Date obj at server
side I lose the ms info).
So I've built a new parameter converter for type Date like this:

@Provider
public class TimestampParamConverter implements ParamConverter<Date> {
	
	/**
	 * Required for REST ws invocation
	 */
	@Override
	public Date fromString(String s) {
		//EXAMPLE TEST
		//example s: Mon May 13 14:21:47 CEST 2013
		SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss
zzz yyyy", Locale.US);
		try {
			return new Date(dateFormat.parse(s).getTime());
		} catch (ParseException e) {
			log.error("Exception building the Date from corresponding string at REST
ws invocation. We return a NULL object. ",e);
			return null;
		}
	}

	@Override
	public String toString(Date arg0) throws IllegalArgumentException {
		// EXAMPLE TEST
		if(arg0 != null){
			return arg0.toString();
		}else{
			return null;
		}
	}
}

and I've added it on client side loading the endPoint in this way:

   JAXRSClientFactoryBean proxyFactory = new JAXRSClientFactoryBean();
   proxyFactory.setServiceClass(clazz);
   proxyFactory.setAddress(address);
   List providers = new ArrayList();
   providers.add(new StringArrayBodyReader());
   providers.add(new TimestampParamConverter());
   proxyFactory.setProviders(providers);
   .....

However I'm not able to arrive in the toString() method at the REST API
invocation....
What am I doing wrong?
Thanks a lot,

Andrea



--
View this message in context: http://cxf.547215.n5.nabble.com/Serialize-parameters-on-client-side-tp5726421p5733516.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Serialize parameters on client side

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi
On 17/04/13 09:15, Raphael Jolivet wrote:
> Hi,
>
> I am developping a REST application with Apache CXF.
>
> On server side, I have setup a ParameterHandler in order to parse Dates with
> a custom format.
> What is the counterpart on client side in order to serialize a parameter ?
>
Up until CXF 2.7.x the only way is to ensure that a custom type's 
toString() method returns the right representation of this parameter,

Starting from CXF 2.7.3 one can use ParameterConverterProvider and 
ParameterConverter pair on the client side too, 
ParameterConverterProvider implementation is registered as a provider 
and returns instances of ParameterConverter:

http://jax-rs-spec.java.net/nonav/2.0-SNAPSHOT/apidocs/javax/ws/rs/ext/ParamConverterProvider.html
http://jax-rs-spec.java.net/nonav/2.0-SNAPSHOT/apidocs/javax/ws/rs/ext/ParamConverter.html

HTH, Sergey


> Thanks
> Raphael
>
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Serialize-parameters-on-client-side-tp5726421.html
> Sent from the cxf-user mailing list archive at Nabble.com.


-- 
Sergey Beryozkin

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

Blog: http://sberyozkin.blogspot.com