How to pass null value in RESTful web service for a GET Method?
Solution 1
The default match of a variable is [^/]+?
(at least one character) you can manually set the match to [^/]*?
and it should also match empty strings.
Just add the format to the userid variable:
@Path("/getProfile/{memberno}/{userid: [^/]*?}/{channelid}")
Solution 2
Please change @Path("/getProfile/{memberno}/{userid}/{channelid}")
@Path("/getProfile/{memberno}/{userid = default}/{channelid}")
so that it will accept null values to user id . You can use Uri templates to specify default values for optional parameters.Please refer
http://msdn.microsoft.com/en-us/library/bb675245.aspx
Related videos on Youtube
Jags
Updated on January 11, 2020Comments
-
Jags over 4 years
I Currently have a RESTFul web Service exposed using CXF. The Method looks like this.
@GET @Path("/getProfile/{memberno}/{userid}/{channelid}") @Consumes("application/xml") public MaintainCustomerProductResponse getUserDetails( @PathParam("memberno") String membernumber, @PathParam("userid") String userid, @PathParam("channelid") String channelid){ //DO Some Logic here. }
This can be accessed via following URL & on submitting valid data I get response.
http://server.com:8080/UserService/getProfile/{memberno}/{userid}/{channelid}
Question: How can I pass null value for
userid
?If I simple ignore the
{userId}
the request is'nt hitting on the server side web service.Example
http://server.com:8080/UserService/getProfile/1001//1
Note: I'm using
SOAPUi
for Testing and without givinguserId
value I see following response onSOAPUI
and Request doesn't hit the server.SOAPUI Response
<data contentType="null" contentLength="0"><![CDATA[]]></data>
-
Jags over 11 yearsThanks Aviram, This helps me to proceed one step Ahead. But if I submit the request with no userid I see that the on the server side the value of channelid is considered as userid and channelId is null. I can rearrange the parameter and get my job done but is it possible to have same order of parameter on server side and still pass null or empty string to userid ?
-
Aviram Segal over 11 yearsThere might be a problem because I didn't put a space after
userid:
can you try adding the space the see what happens ? -
Jags over 11 yearsWithout space it worked! I mean it allowed me to send "" (emptystring) which I assume/convert that into null on server side. BUT on Submitting below url to server. What's happening is FirstParameter = membernumber is taking 51 SecondParameter = userId is taking 1 and ThirdParameter = channelId is taking null. I.e., the channelID (3rd parameter) is mapped to userId (Second Parameter) if null or emptyString (51//1) is passed. boardingservice/comint/webservice/getProfile/51//1 Somthing like below stackoverflow.com/questions/11266349/…
-
Aviram Segal over 11 yearsI got that, but what does it do without the space ? i read somewhere it could have an effect