Java 使用不同参数调用REST-WS

Java 使用不同参数调用REST-WS,java,web-services,rest,Java,Web Services,Rest,在从ajax前端调用REST WS时,我有一个基本的疑问。我将从ajax调用WS作为: url: self.GET_GOAL_VIEW_URL + '?userEmail=' + eMail, 或作为: url: self.GET_GOAL_VIEW_URL, 现在,在显式传递userEmail参数的情况下,我需要在后端服务代码中使用userEmail,但是如果调用中没有userEmail,我需要使用另一个名为userId的参数,该参数由代理添加到调用中 因此,我不知道如何编写WS-API,

在从ajax前端调用REST WS时,我有一个基本的疑问。我将从ajax调用WS作为:

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

url: self.GET_GOAL_VIEW_URL,
现在,在显式传递userEmail参数的情况下,我需要在后端服务代码中使用userEmail,但是如果调用中没有userEmail,我需要使用另一个名为userId的参数,该参数由代理添加到调用中


因此,我不知道如何编写WS-API,以便它接受这个参数或那个参数,基于ajax请求中使用的参数。非常感谢您在这方面的帮助

您可以将参数作为查询参数或正文参数传递。 您没有提到您将在后端使用哪个REST框架,因此假设您将使用jersey,代码应如下所示:

带查询参数:

@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()
    }
}
@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()
    }
}
带有主体参数:

@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()
    }
}
@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()
    }
}

当然,您需要指定返回的内容类型,并根据需要更改方法类型。

您可以将参数作为查询参数或正文参数传递。 您没有提到您将在后端使用哪个REST框架,因此假设您将使用jersey,代码应如下所示:

带查询参数:

@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()
    }
}
@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()
    }
}
带有主体参数:

@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()
    }
}
@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()
    }
}

当然,您需要指定要返回的内容类型,并在需要时更改方法类型。

您需要以这样一种方式开发web服务,即它接受这两个参数。参数可以是可选的,业务处理逻辑将基于请求中接收的参数。为什么不将一个包含所有可能参数的
JSON
字符串传递给
REST
服务,并根据请求中传递的内容在服务中处理它们。正如@AniketV所提到的,您需要设计web服务来接受
userMail
userId
。您使用什么技术来开发后端web服务?您需要以这样一种方式来开发web服务,即它接受这两个参数。参数可以是可选的,业务处理逻辑将基于请求中接收的参数。为什么不将一个包含所有可能参数的
JSON
字符串传递给
REST
服务,并根据请求中传递的内容在服务中处理它们。正如@AniketV所提到的,您需要设计web服务来接受
userMail
userId
。您使用什么技术来开发后端web服务?非常感谢您的回复。我正在使用jersey并发送一个查询参数请求。所以,如果我在REST API方法中有两个参数,正如您上面所给出的,并且如果我在ajax调用中只发送一个参数或根本不发送任何参数,那就好了,不是吗?它将查看这些参数中是否有任何参数在ajax调用中可用,并相应地将请求中的参数名称与WS-method参数相匹配并进行设置。@RohitGupta正确-JAX-RS(jersey)查询参数不是必需的。如果您的客户端不发送它们,它们将是空的。非常感谢您的回复。我正在使用jersey并发送一个查询参数请求。所以,如果我在REST API方法中有两个参数,正如您上面所给出的,并且如果我在ajax调用中只发送一个参数或根本不发送任何参数,那就好了,不是吗?它将查看这些参数中是否有任何参数在ajax调用中可用,并相应地将请求中的参数名称与WS-method参数相匹配并进行设置。@RohitGupta正确-JAX-RS(jersey)查询参数不是必需的。如果您的客户端不发送它们,它们将为空。