How to pass null value in RESTful web service for a GET Method?

27,287

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

Share:
27,287

Related videos on Youtube

Jags
Author by

Jags

Updated on January 11, 2020

Comments

  • Jags
    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 giving userId value I see following response on SOAPUI and Request doesn't hit the server.

    SOAPUI Response

    <data contentType="null" contentLength="0"><![CDATA[]]></data>
    
  • Jags
    Jags over 11 years
    Thanks 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
    Aviram Segal over 11 years
    There might be a problem because I didn't put a space after userid: can you try adding the space the see what happens ?
  • Jags
    Jags over 11 years
    Without 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
    Aviram Segal over 11 years
    I got that, but what does it do without the space ? i read somewhere it could have an effect

Related