Jags
Jags

Reputation: 71

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

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>

Upvotes: 5

Views: 31394

Answers (6)

hariprasad
hariprasad

Reputation: 585

There is needed more detailed describe all cases, which are possible. Look on the exapmple based on Spring Framework:

        @RestController
        @RequestMapping("/misc")
        public class MiscRestController {
    ...
            @RequestMapping(value = { "/getevidencecounts/{userId}",
                    "/getevidencecounts/{userId}/{utvarVSId = default}" }, method = RequestMethod.GET)
            public ResponseEntity<EvidenceDokumentuCountTO> getEvidenceCounts(
                    @PathVariable(value = "userId", required = true) Long userId,
                    @PathVariable(value = "utvarVSId", required = false) Long utvarVSId) {
...

We needed to use path like .../misc/1 (1), .../misc/1/25 (2), .../misc/1/null (3). Path 1 is represented /getevidencecounts/{userId}. path 2 and 3 /getevidencecounts/{userId}/{utvarVSId = default}. If you are sure, that default value is null, you can also sustitute in declaration = default by = null value.

Upvotes: 0

utrucceh
utrucceh

Reputation: 1116

@GET
@Path("qwer/{z}&{x:.*?}&{c}")
@Produces(MediaType.TEXT_PLAIN)
public String withNullableX(
        @PathParam("z") Integer z,
        @PathParam("x") Integer x,
        @PathParam("c") Integer c
) {
    return z + "" + x + "" + c;
}

When you call with
http://localhost:8080/asdf/qwer/1&&3
you will see:
1null3

Upvotes: 0

Pruthvidhar Reddy
Pruthvidhar Reddy

Reputation: 129

You could use URL encoding, pass %00. Refer this.

Example: http://server.com:8080/UserService/getProfile/1001/%00/1

Upvotes: 0

Luas denis
Luas denis

Reputation: 31

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

Upvotes: 2

Jags
Jags

Reputation: 71

I got it resolved with a Work Around. I created another Method that takes only two parameter. Now Based on Request URL different method is called

Upvotes: 0

Aviram Segal
Aviram Segal

Reputation: 11120

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}")

Upvotes: 7

Related Questions