简体   繁体   English

@Produces,@ GET,@ Path和@QueryParam在函数定义之前的重要性

[英]Significance of @Produces, @GET, @Path and @QueryParam before function definition

I am new to java. 我是java的新手。 I was looking the below code. 我在看下面的代码。

@Produces("text/xml")
@GET
@Path("/xml/search")
public Object searchXML(@QueryParam("query") String query,
        @QueryParam("granularity") String granularity) {
    return search(query, granularity);
}

I couldn't understand the uses of @Produces, @GET, @Path and @QueryParam before function definition in above code. 在上面的代码中,我无法理解@Produces,@ GET,@ Path和@QueryParam在函数定义之前的用法。 Can anybody put some light on this. 任何人都可以对此有所了解。 Thanks 谢谢

Those anotations are defined by JAX-RS , a Standard for RESTful Web Services. 这些anotations由JAX-RS定义, JAX-RS是RESTful Web服务的标准。

In the example above, it mean the method will handle a: 在上面的例子中,它意味着该方法将处理:

  • GET request GET请求

  • on path "/xml/search" 在路径“/ xml / search”

  • and map the query argument "query" to String query argument, 并将查询参数“query”映射到String查询参数,

  • as well as the "granularity" to granularity 以及粒度的“粒度”

  • the resulting content-type will be "text/xml" 生成的内容类型将是“text / xml”

(and it will probably call a custom marshaller for this) (它可能会为此调用一个自定义编组器)

(see this page for a reference) (参见本页参考)

A producer method is a method that generates an object that can then be injected. 生成器方法是一种生成可以随后注入的对象的方法。 Link When you want to inject an object that is not itself a bean When the concrete type of the object to be injected may vary at runtime When the object requires some custom initialization that the bean constructor does not perform 链接当你想要注入一个本身不是bean的对象当要注入的对象的具体类型可能在运行时变化时当对象需要一些自定义初始化时,bean构造函数不执行

@GET: Transmits a representation of the resource identified by the URI to the client. @GET:将URI标识的资源表示传输给客户端。 The format might be HTML, plain text, JPEG, and so on. 格式可能是HTML,纯文本,JPEG等。 See How to Transmit a Representation of the Resource (@GET). 请参见如何传输资源的表示(@GET)。 Link 链接

@Path(param) : @PathParam is a parameter annotation which allows you to map variable URI path fragments into your method call. @Path(param):@ PathParam是一个参数注释,它允许您将变量URI路径片段映射到方法调用中。 Link 链接

can use @QueryParam annotation to inject URI query parameter into Java method Link 可以使用@QueryParam注释将URI查询参数注入Java方法链接

@Produces annotation is to specify the MIME media types of representations a resource can produce and send back to the client.here mediatype is an text rather xml can be recieved, @Produces注释是指定资源可以生成并发送回客户端的表示的MIME媒体类型。其中mediatype是一个文本而不是xml可以被收到,

@GET is an HTTP Methods like @PUT, @POST, @DELETE @GET是一个HTTP方法,如@ PUT,@ POST,@ DELETE

@Path annotation's value is a relative URI path .In this the function call will be from service is through the path as /xml/search/ to the function with the params. @Path annotation的值是一个相对的URI路径。这个函数调用将来自service的路径为/xml/search/到params的函数。

@QueryParam is to extract query parameters from the Query component of the request URL, in this receives the param as strings from the URL . @QueryParam是从请求URL的Query组件中提取查询参数,在此接收来自URL的param作为字符串。

Document Reference more info 文档参考 更多信息

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

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