简体   繁体   English

Jersey JAX-RS资源的可能过载列表

[英]List of possible overloads for Jersey JAX-RS resources

I'm using Jersey to implement a JAX-RS resource. 我正在使用Jersey来实现JAX-RS资源。 I've seen lots of different examples on Stack Overflow, various blogs and the Jersey User Guide . 我在Stack Overflow,各种博客和Jersey用户指南中看到了许多不同的示例。

I would like to know what the different overloads can be for a given resource handler. 我想知道给定资源处理程序可以有哪些不同的重载。 Is there a single source where these are documented? 是否有记录这些文献的单一来源?

For example, the following handles an HTTP POST request. 例如,以下内容处理HTTP POST请求。 The request body is captured as a MultivaluedMap . 请求主体被捕获为MultivaluedMap

@POST
public Response httpPostRequest(MultivaluedMap<String, String> body)
{
    ...
}

Alternatively, the following overload captures the body as a single String . 或者,以下重载将主体捕获为单个String

@POST
public Response httpPostRequest(String body)
{
    ...
}

There are other overloads too. 也有其他过载。 How many are there and where are they documented? 有多少个文件,在哪里记录?

It is just a normal Java method that has one or more annotations associated with it. 它只是一个普通的Java方法,具有一个或多个与之关联的注释。 The signature of the method has no particular constraints placed on it by Jersey. 该方法的签名不受Jersey的特别限制。

Having said that, you will want to make sure that the various annotations (eg, @Produces , @Consumes , @PathParam , @QueryParam ) are applied to data types that Jersey knows how to map. 说了这么多,你会希望确保各种注释(例如, @Produces@Consumes@PathParam@QueryParam )应用于数据类型泽西知道如何映射。 For example, Jersey has no problem with mapping @PathParam to String or long . 例如,Jersey将@PathParam映射到Stringlong没有问题。 Jersey can also work with Java classes that have JAXB annotations, so your method signature can include a JAXB type combined with @Consumes(MediaType.APPLICATION_XML) and Jersey will convert the request content from an XML document to the JAXB Java class. Jersey也可以与具有JAXB批注的Java类一起使用,因此您的方法签名可以包括JAXB类型并与@Consumes(MediaType.APPLICATION_XML) ,Jersey会将请求内容从XML文档转换为JAXB Java类。

For example: 例如:

@GET
@Produces(MediaType.APPLICATION_XML)
@Path("somepath")
public Foos getFoosByQuery(@PathParam("businessName") String businessName,
        @PathParam("businessUnitName") String businessUnitName, @PathParam("fileType") String fileType,
        @QueryParam("from") String fromString, @QueryParam("to") String toString,
        @DefaultValue("10") @QueryParam("interval") int intervalMinutes,
        @DefaultValue("1000") @QueryParam("limit") int limit,
        @DefaultValue("false") @QueryParam("errors") boolean errors) {

Here, we see that we have many parameters (with types String , int and boolean ) and a return type that is a JAXB-annotated POJO. 在这里,我们看到我们有很多参数(类型为Stringintboolean ),返回类型为带有JAXB注释的POJO。 Jersey pulls the @PathParam values from the path, the @QueryParam values from the query string and converts the return value into an XML document and includes it as the content of the response. 泽西拉动@PathParam从路径值,则@QueryParam值从查询字符串和返回值转换成XML文档,并包括它作为响应的内容。

I will also note that the name of the method can be anything we want, so the concept of "overloading" is orthogonal to Jersey. 我还将注意到,该方法的名称可以是我们想要的任何名称,因此“重载”的概念与Jersey正交。 The normal Java overloading rules apply. 正常的Java重载规则适用。

It should be obvious from this example that you cannot enumerate all of the possible "overloads" that you can use with Jersey. 从此示例可以明显看出,您无法列举可以与Jersey一起使用的所有可能的“重载”。

Perhaps a different question regarding all of the possible type mappings that Jersey can do would be more in line with what you are looking for. 对于Jersey可以完成的所有可能的类型映射,一个不同的问题也许与您要查找的内容更加一致。

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

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