简体   繁体   中英

Calling REST WS with different parameters

I have a basic doubt while calling a REST WS from the ajax frontend. I am calling the WS from ajax as:

url: self.GET_GOAL_VIEW_URL + '?userEmail=' + eMail,

or as:

url: self.GET_GOAL_VIEW_URL,

Now in cases where the userEmail parameters would be passed explicitly, I need to use userEmail in the backend service code, but if the userEmail is absent in a call, I need to use another parameter, called userId, which is being added to the call by the proxy.

So I am not getting how to write the WS API so that it takes either this parameter or that, based on which one is used in the ajax request. Would be grateful for your assistance on this.

You can pass the parameters as query params or body params. You haven't mentioned which REST framework you will use on the backend so assuming you will use jersey the code should look as follows:

With query parameters:

@POST
@Path("/somepath")
public Response doSomething(@QueryParam("userEmail") String userEmail, @QueryParam("userId") String userId) {
    if(userEmail != null && !userEmail.equals("")) {
        //use email address
    } else if(userId != null && !userId.equals("")) {
        //use user id
    } else {
        throw new RuntimeException()
    }
}

With body parameters:

@POST
@Path("/somepath")
public Response doSomething(userDTO user) {
    if(user.getUserEmail() != null && !user.getUserEmail().equals("")) {
        //use email address
    } else if(user.getUserId() != null && !user.getUserId().equals("")) {
        //use user id
    } else {
        throw new RuntimeException()
    }
}

Of course you'll need to specify what content type you're returning, and change the method type if needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM