简体   繁体   English

JAX-RS资源方法签名

[英]JAX-RS resource method signatures

I'm using Jersey 2.19 to implement a REST API. 我正在使用Jersey 2.19来实现REST API。

I'd like to know how I find out from the Jersey user guide or other specification how I'm supposed to know what the signature of my JAX-RS resource should be. 我想知道如何从Jersey用户指南或其他规范中找出我应该如何知道我的JAX-RS资源的签名。

Eg for a resource that handles POST requests I've experimented with the following different signatures using examples I've found. 例如,为处理POST请求的资源,我使用发现的示例尝试了以下不同的签名。

public Response myResource()
public Response myResource(String param)

Both of these are valid in that they compile and run and the method is called under the right conditions. 两者都是有效的,因为它们可以编译并运行,并且该方法在正确的条件下被调用。

Can anyone tell me where it is specified what the signatures should be and what the parameters mean? 谁能告诉我签名应该在什么地方以及参数的含义? It seems like a straightforward question but I can't find the answer. 这似乎是一个简单的问题,但我找不到答案。

As you are saying its a POST request , so it should recieve some data from the Request. 正如您所说的POST请求一样,它应该从Request中接收一些数据。 So you should expect something in Parameter. 因此,您应该期望Parameter中有一些东西。 public Response myResource(String param) But the type of parameter should depend upon actually @Consumes annotation like :- public Response myResource(String param)但是参数的type实际上应该取决于@Consumes注释,例如:

@Consumes(MediaType.APPLICATION_JSON) : This expects a JSON input OR @Consumes(MediaType.APPLICATION_XML) : This expects a XML input OR @Consumes(MediaType.TEXT_PLAIN) : This expects a String plain text input @Consumes(MediaType.APPLICATION_JSON) :这需要JSON输入,或者@Consumes(MediaType.APPLICATION_XML) :这需要XML输入;或者@Consumes(MediaType.TEXT_PLAIN) :这需要String纯文本输入

You annotate your Methods like described in the official documentation . 您可以按照官方文档中的说明对方法进行注释。 Also, do not forget to annotate the service-class with @Path 另外,不要忘了用@Path注释服务类。

@Path("MyService")
public class MyService
{
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/User")
    public List<User> getUser()
    {
        //Return all users
    }

    //Inserts new User in JSON Format
    @Get
    @Path("/User/UserId/{userid}")
    public User getUserById(@PathParam("userid") String userid)
    {
        //Find User with ID in Database and return it
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public User getUserById(User user)
    {
        //add user to your Database or something
    }
}

If you now want to get all users in json format you have to call: http://ip-address/MyService/user 如果现在要以json格式获取所有用户,则必须调用: http://ip-address/MyService/user

There is an exact answer to your question, but gird your loins. 您的问题有确切答案,但会束缚您的腰部。 Because if the Jersey docs are overly vague these are in the extreme opposite direction: written by someone showing off their PhD in abstract algebra it looks to me. 因为如果Jersey的文档过于模糊,那么它们的方向就截然相反:由某人用抽象代数炫耀其博士学位的作品所组成。

The answer to everything is in the JAX-RS spec, of which Jersey is an implementation . 一切的答案都在JAX-RS规范中,Jersey是其实现 You can download it here as PDF (after you sign away your soul) 您可以在此处将其下载为PDF (在您注销灵魂之后)

The specific answer to how one of those methods is selected instead of the other, is too detailed for me to paste in here, but it's under section " 3.7.2 Request Matching " 对于如何选择其中一种方法而不是另一种方法的具体答案,对于我来说太详细了,无法在此处粘贴,但在“ 3.7.2请求匹配 ”部分中

I won't even try to paste in the mathematical rules used to set up the list of potential methods to match a request, then select from among them. 我什至不会尝试粘贴用于设置与请求匹配的潜在方法列表的数学规则,然后从中进行选择。 There's no chance of getting them formatted readably in SO. 没有机会用SO可读地格式化它们。

For your more general questions, the section " 3.3 Resource Methods " is much more accessible. 对于您的更一般性问题,“ 3.3资源方法 ”部分将更易于访问。 Here are a few choice excerpts: 以下是一些选择摘录:

3.3 Resource Methods 3.3资源方法

... JAX-RS defines a set of request method designators for the common HTTP methods: @GET, @POST, @PUT, @DELETE, @HEAD and @OPTIONS. ... JAX-RS为常见的HTTP方法定义了一组请求方法标识符:@ GET,@ POST,@ PUT,@ DELETE,@ HEAD和@OPTIONS。 ... ...

3.3.1 Visibility: Only public methods may be exposed as resource methods. 3.3.1可见性:只有公共方法才可以公开为资源方法。 ... ...

3.3.2 Parameters: Resource methods MUST have at most one entity parameter ... 3.3.2参数:资源方法必须最多具有一个实体参数...

3.3.3 Return Type: Resource methods MAY return void, Response, GenericEntity, or another Java type... 3.3.3返回类型:资源方法可以返回void,Response,GenericEntity或其他Java类型...

etc, etc. 等等等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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