You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Maxim Solodovnik <so...@gmail.com> on 2015/12/16 11:04:56 UTC

RS service method accepting 2 parameters

Hello All,

I'm currently trying to write RS service accepting 2 objects of different
types as parameters:

ex. code:

@WebService(serviceName="org.apache.openmeetings.webservice.UserWebService")
@Features(features = "org.apache.cxf.feature.LoggingFeature")
@Produces({MediaType.APPLICATION_JSON})
@Path("/user")
public class UserWebService {
@POST
@Path("/hash")
public ServiceResult getRoomHash(
@FormParam("user") BeanA user
, @FormParam("options") BeanB options
) throws ServiceException
{}
}

and I'm trying to invoke this method from JUnit test:


ExternalUserDTO user = new ExternalUserDTO();
RoomOptionsDTO options = new RoomOptionsDTO();
Map<String, List<Object>> map = new LinkedHashMap<>();
map.put("user", Arrays.asList((Object)user));
map.put("options", Arrays.asList((Object)options));
Response resp = getClient(USER_SERVICE_URL)
.path("/hash")
.form(map);
//throws no body wrapper for LinkedHashMap class

I cannot use .form(new Form().....) due to it can only accepts string
parameters

can someone please point me to an example or provide with the hint how this
can be implemented

Thanks in advance


-- 
WBR
Maxim aka solomax

Re: RS service method accepting 2 parameters

Posted by Maxim Solodovnik <so...@gmail.com>.
OK
Thanks for the help Sergey!

On Thu, Dec 17, 2015 at 4:10 PM, Sergey Beryozkin <sb...@gmail.com>
wrote:

> Hi
> I may have got confused
>
> So you need a form sequence where every value is a complex property ?
> Unfortunately JAX-RS does not support helping in this case, but you can
> have a method signature with multiple @FormParam() parameters, and use a
> proxy based API, so in this case by registering a custom
> MyParamConverterProvider you can get that to/from String conversion code
> moved out of the beans to this provider...
>
> Thanks, Sergey
>
>
> On 17/12/15 05:09, Maxim Solodovnik wrote:
>
>> Hello Sergey,
>>
>> thanks for the quick response,
>> maybe you have some example/test I can take a look at? cause currently I
>> need to create methods like:
>>
>> Response resp = getClient(USER_SERVICE_URL)
>> .path("/hash")
>> .query("sid", r.getMessage())
>> .form(new Form().param("user", new
>> JSONObject(user).toString()).param("options", new
>> JSONObject(options).toString()));
>>
>> public static BeanA fromString(String s) {
>> JSONObject o = new JSONObject(s);
>> BeanA b = new BeanA();
>> b.prop1 = o.getString("prop1");
>> b.prop2 = o.getString("prop2");
>> b.prop3 = o.getString("prop3");
>> b.prop4 = o.getString("prop4");
>> return b;
>> }
>>
>> in my beans, which I believe should be done somehow automatically
>>
>> without this code I'm getting
>> Parameter Class org.apache.openmeetings.package.BeanA has no constructor
>> with single String parameter, static valueOf(String) or fromString(String)
>> methods
>>
>>
>>
>> On Wed, Dec 16, 2015 at 4:23 PM, Sergey Beryozkin <sb...@gmail.com>
>> wrote:
>>
>> Hi Max
>>>
>>> It is unfortunate Form accepts parameters with String values, I think we
>>> missed it should be Object instead given that ParamConverterProvider is
>>> available in JAX-RS 2.0.
>>>
>>> form() can deal with MultipartMap - CXF ships MetadataMap which
>>> implements
>>> it, and JAX-RS 2.0 ships a simple implementation too:
>>>
>>>
>>>
>>> https://jax-rs-spec.java.net/nonav/2.0-rev-a/apidocs/javax/ws/rs/core/MultivaluedHashMap.html
>>>
>>> You'll still need registering ParamConverterProvider if values are not
>>> String and the beans do not override toString()
>>>
>>> HTH, Sergey
>>>
>>>
>>>
>>>
>>> It has to be MultivaluedMap, JAX-RS
>>>
>>> On 16/12/15 10:04, Maxim Solodovnik wrote:
>>>
>>> Hello All,
>>>>
>>>> I'm currently trying to write RS service accepting 2 objects of
>>>> different
>>>> types as parameters:
>>>>
>>>> ex. code:
>>>>
>>>>
>>>>
>>>> @WebService(serviceName="org.apache.openmeetings.webservice.UserWebService")
>>>> @Features(features = "org.apache.cxf.feature.LoggingFeature")
>>>> @Produces({MediaType.APPLICATION_JSON})
>>>> @Path("/user")
>>>> public class UserWebService {
>>>> @POST
>>>> @Path("/hash")
>>>> public ServiceResult getRoomHash(
>>>> @FormParam("user") BeanA user
>>>> , @FormParam("options") BeanB options
>>>> ) throws ServiceException
>>>> {}
>>>> }
>>>>
>>>> and I'm trying to invoke this method from JUnit test:
>>>>
>>>>
>>>> ExternalUserDTO user = new ExternalUserDTO();
>>>> RoomOptionsDTO options = new RoomOptionsDTO();
>>>> Map<String, List<Object>> map = new LinkedHashMap<>();
>>>> map.put("user", Arrays.asList((Object)user));
>>>> map.put("options", Arrays.asList((Object)options));
>>>> Response resp = getClient(USER_SERVICE_URL)
>>>> .path("/hash")
>>>> .form(map);
>>>> //throws no body wrapper for LinkedHashMap class
>>>>
>>>> I cannot use .form(new Form().....) due to it can only accepts string
>>>> parameters
>>>>
>>>> can someone please point me to an example or provide with the hint how
>>>> this
>>>> can be implemented
>>>>
>>>> Thanks in advance
>>>>
>>>>
>>>>
>>>>
>>> --
>>> Sergey Beryozkin
>>>
>>> Talend Community Coders
>>> http://coders.talend.com/
>>>
>>>
>>
>>
>>
>
> --
> Sergey Beryozkin
>
> Talend Community Coders
> http://coders.talend.com/
>



-- 
WBR
Maxim aka solomax

Re: RS service method accepting 2 parameters

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi
I may have got confused

So you need a form sequence where every value is a complex property ?
Unfortunately JAX-RS does not support helping in this case, but you can 
have a method signature with multiple @FormParam() parameters, and use a 
proxy based API, so in this case by registering a custom 
MyParamConverterProvider you can get that to/from String conversion code 
moved out of the beans to this provider...

Thanks, Sergey

On 17/12/15 05:09, Maxim Solodovnik wrote:
> Hello Sergey,
>
> thanks for the quick response,
> maybe you have some example/test I can take a look at? cause currently I
> need to create methods like:
>
> Response resp = getClient(USER_SERVICE_URL)
> .path("/hash")
> .query("sid", r.getMessage())
> .form(new Form().param("user", new
> JSONObject(user).toString()).param("options", new
> JSONObject(options).toString()));
>
> public static BeanA fromString(String s) {
> JSONObject o = new JSONObject(s);
> BeanA b = new BeanA();
> b.prop1 = o.getString("prop1");
> b.prop2 = o.getString("prop2");
> b.prop3 = o.getString("prop3");
> b.prop4 = o.getString("prop4");
> return b;
> }
>
> in my beans, which I believe should be done somehow automatically
>
> without this code I'm getting
> Parameter Class org.apache.openmeetings.package.BeanA has no constructor
> with single String parameter, static valueOf(String) or fromString(String)
> methods
>
>
>
> On Wed, Dec 16, 2015 at 4:23 PM, Sergey Beryozkin <sb...@gmail.com>
> wrote:
>
>> Hi Max
>>
>> It is unfortunate Form accepts parameters with String values, I think we
>> missed it should be Object instead given that ParamConverterProvider is
>> available in JAX-RS 2.0.
>>
>> form() can deal with MultipartMap - CXF ships MetadataMap which implements
>> it, and JAX-RS 2.0 ships a simple implementation too:
>>
>>
>> https://jax-rs-spec.java.net/nonav/2.0-rev-a/apidocs/javax/ws/rs/core/MultivaluedHashMap.html
>>
>> You'll still need registering ParamConverterProvider if values are not
>> String and the beans do not override toString()
>>
>> HTH, Sergey
>>
>>
>>
>>
>> It has to be MultivaluedMap, JAX-RS
>>
>> On 16/12/15 10:04, Maxim Solodovnik wrote:
>>
>>> Hello All,
>>>
>>> I'm currently trying to write RS service accepting 2 objects of different
>>> types as parameters:
>>>
>>> ex. code:
>>>
>>>
>>> @WebService(serviceName="org.apache.openmeetings.webservice.UserWebService")
>>> @Features(features = "org.apache.cxf.feature.LoggingFeature")
>>> @Produces({MediaType.APPLICATION_JSON})
>>> @Path("/user")
>>> public class UserWebService {
>>> @POST
>>> @Path("/hash")
>>> public ServiceResult getRoomHash(
>>> @FormParam("user") BeanA user
>>> , @FormParam("options") BeanB options
>>> ) throws ServiceException
>>> {}
>>> }
>>>
>>> and I'm trying to invoke this method from JUnit test:
>>>
>>>
>>> ExternalUserDTO user = new ExternalUserDTO();
>>> RoomOptionsDTO options = new RoomOptionsDTO();
>>> Map<String, List<Object>> map = new LinkedHashMap<>();
>>> map.put("user", Arrays.asList((Object)user));
>>> map.put("options", Arrays.asList((Object)options));
>>> Response resp = getClient(USER_SERVICE_URL)
>>> .path("/hash")
>>> .form(map);
>>> //throws no body wrapper for LinkedHashMap class
>>>
>>> I cannot use .form(new Form().....) due to it can only accepts string
>>> parameters
>>>
>>> can someone please point me to an example or provide with the hint how
>>> this
>>> can be implemented
>>>
>>> Thanks in advance
>>>
>>>
>>>
>>
>> --
>> Sergey Beryozkin
>>
>> Talend Community Coders
>> http://coders.talend.com/
>>
>
>
>


-- 
Sergey Beryozkin

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

Re: RS service method accepting 2 parameters

Posted by Maxim Solodovnik <so...@gmail.com>.
Hello Sergey,

thanks for the quick response,
maybe you have some example/test I can take a look at? cause currently I
need to create methods like:

Response resp = getClient(USER_SERVICE_URL)
.path("/hash")
.query("sid", r.getMessage())
.form(new Form().param("user", new
JSONObject(user).toString()).param("options", new
JSONObject(options).toString()));

public static BeanA fromString(String s) {
JSONObject o = new JSONObject(s);
BeanA b = new BeanA();
b.prop1 = o.getString("prop1");
b.prop2 = o.getString("prop2");
b.prop3 = o.getString("prop3");
b.prop4 = o.getString("prop4");
return b;
}

in my beans, which I believe should be done somehow automatically

without this code I'm getting
Parameter Class org.apache.openmeetings.package.BeanA has no constructor
with single String parameter, static valueOf(String) or fromString(String)
methods



On Wed, Dec 16, 2015 at 4:23 PM, Sergey Beryozkin <sb...@gmail.com>
wrote:

> Hi Max
>
> It is unfortunate Form accepts parameters with String values, I think we
> missed it should be Object instead given that ParamConverterProvider is
> available in JAX-RS 2.0.
>
> form() can deal with MultipartMap - CXF ships MetadataMap which implements
> it, and JAX-RS 2.0 ships a simple implementation too:
>
>
> https://jax-rs-spec.java.net/nonav/2.0-rev-a/apidocs/javax/ws/rs/core/MultivaluedHashMap.html
>
> You'll still need registering ParamConverterProvider if values are not
> String and the beans do not override toString()
>
> HTH, Sergey
>
>
>
>
> It has to be MultivaluedMap, JAX-RS
>
> On 16/12/15 10:04, Maxim Solodovnik wrote:
>
>> Hello All,
>>
>> I'm currently trying to write RS service accepting 2 objects of different
>> types as parameters:
>>
>> ex. code:
>>
>>
>> @WebService(serviceName="org.apache.openmeetings.webservice.UserWebService")
>> @Features(features = "org.apache.cxf.feature.LoggingFeature")
>> @Produces({MediaType.APPLICATION_JSON})
>> @Path("/user")
>> public class UserWebService {
>> @POST
>> @Path("/hash")
>> public ServiceResult getRoomHash(
>> @FormParam("user") BeanA user
>> , @FormParam("options") BeanB options
>> ) throws ServiceException
>> {}
>> }
>>
>> and I'm trying to invoke this method from JUnit test:
>>
>>
>> ExternalUserDTO user = new ExternalUserDTO();
>> RoomOptionsDTO options = new RoomOptionsDTO();
>> Map<String, List<Object>> map = new LinkedHashMap<>();
>> map.put("user", Arrays.asList((Object)user));
>> map.put("options", Arrays.asList((Object)options));
>> Response resp = getClient(USER_SERVICE_URL)
>> .path("/hash")
>> .form(map);
>> //throws no body wrapper for LinkedHashMap class
>>
>> I cannot use .form(new Form().....) due to it can only accepts string
>> parameters
>>
>> can someone please point me to an example or provide with the hint how
>> this
>> can be implemented
>>
>> Thanks in advance
>>
>>
>>
>
> --
> Sergey Beryozkin
>
> Talend Community Coders
> http://coders.talend.com/
>



-- 
WBR
Maxim aka solomax

Re: RS service method accepting 2 parameters

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

It is unfortunate Form accepts parameters with String values, I think we 
missed it should be Object instead given that ParamConverterProvider is 
available in JAX-RS 2.0.

form() can deal with MultipartMap - CXF ships MetadataMap which 
implements it, and JAX-RS 2.0 ships a simple implementation too:

https://jax-rs-spec.java.net/nonav/2.0-rev-a/apidocs/javax/ws/rs/core/MultivaluedHashMap.html

You'll still need registering ParamConverterProvider if values are not 
String and the beans do not override toString()

HTH, Sergey




It has to be MultivaluedMap, JAX-RS
On 16/12/15 10:04, Maxim Solodovnik wrote:
> Hello All,
>
> I'm currently trying to write RS service accepting 2 objects of different
> types as parameters:
>
> ex. code:
>
> @WebService(serviceName="org.apache.openmeetings.webservice.UserWebService")
> @Features(features = "org.apache.cxf.feature.LoggingFeature")
> @Produces({MediaType.APPLICATION_JSON})
> @Path("/user")
> public class UserWebService {
> @POST
> @Path("/hash")
> public ServiceResult getRoomHash(
> @FormParam("user") BeanA user
> , @FormParam("options") BeanB options
> ) throws ServiceException
> {}
> }
>
> and I'm trying to invoke this method from JUnit test:
>
>
> ExternalUserDTO user = new ExternalUserDTO();
> RoomOptionsDTO options = new RoomOptionsDTO();
> Map<String, List<Object>> map = new LinkedHashMap<>();
> map.put("user", Arrays.asList((Object)user));
> map.put("options", Arrays.asList((Object)options));
> Response resp = getClient(USER_SERVICE_URL)
> .path("/hash")
> .form(map);
> //throws no body wrapper for LinkedHashMap class
>
> I cannot use .form(new Form().....) due to it can only accepts string
> parameters
>
> can someone please point me to an example or provide with the hint how this
> can be implemented
>
> Thanks in advance
>
>


-- 
Sergey Beryozkin

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