OffsetDateTime产生“未找到类型为public javax.ws.rs.core.response的参数的注入源".在GET方法中 [英] OffsetDateTime yielding "No injection source found for a parameter of type public javax.ws.rs.core.response" in GET method

查看:124
本文介绍了OffsetDateTime产生“未找到类型为public javax.ws.rs.core.response的参数的注入源".在GET方法中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下GET REST方法:

I have the following GET REST method:

import java.time.OffsetDateTime;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import com.product.rest.api.TransactionsApi;
import com.product.rest.model.Transaction;

@Path("/transactions")

@Api(description = "the transactions API")
@Consumes({ "application/json" })
@Produces({ "application/json" })
public class TransactionsApiImpl extends TransactionsApi {

    @GET

    @Consumes({ "application/json" })
    @Produces({ "application/json" })
    @ApiOperation(value = "", notes = "Get all transactions", response =     Transaction.class, responseContainer = "List", tags = {})
    @ApiResponses(
        value = { @ApiResponse(code = 200, message = "OK", response =     Transaction.class, responseContainer = "List"),
            @ApiResponse(code = 400, message = "Bad Request", response =     Transaction.class, responseContainer = "List"),
            @ApiResponse(code = 404, message = "Not Found", response =     Transaction.class, responseContainer = "List"),
            @ApiResponse(code = 500, message = "Internal Server Error",     response = Transaction.class, responseContainer = "List") })
    @Override
    public Response transactionsGet(
        @HeaderParam("tok") String tok,
        @QueryParam("param1") Integer param1,
        @QueryParam("param2") String param2,
        @QueryParam("param3") OffsetDateTime param3,
        @QueryParam("param4") OffsetDateTime param4,
        @QueryParam("param5") Integer param5,
        @QueryParam("param6") Integer param6,
        @QueryParam("param7") String param7) {
        return Response.ok().entity("Success!").build();
    }

TransactionsApi是使用Swagger Codegen生成的实现,Transaction模型类也是如此.我在此类中还有其他几个函数,但是每当我不评论GET /transactions函数时,就会收到以下错误:

The TransactionsApi is a generated implementation using Swagger Codegen, as is the Transaction model class. I have several other functions in this class, but whenever I leave the GET /transactions function uncommented, I receive the following error:

WARN [Thread-1] (ContextHandler.java:2175) - unavailable
org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.

[[FATAL] No injection source found for a parameter of type public javax.ws.rs.core.Response     
com.product.rest.impl.v1.TransactionsApiImpl.transactionsGet(java.lang.String,java.lang.Integer,java.lang.String,java.time.OffsetDateTime,java.time.OffsetDateTime,java.lang.Integer,java.lang.Integer,java.lang.String) at index 3.; source='ResourceMethod{httpMethod=GET, consumedTypes=[application/json], producedTypes=[application/json], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class com.product.rest.impl.v1.TransactionsApiImpl, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@7df78e88]}, definitionMethod=public javax.ws.rs.core.Response

我发现的所有其他类似问题都与MultiPart Data和文件上传有关,而我正在提出一个简单的GET请求.其他也使用javax.ws.rs.code.Response类的功能都没有此问题,并且服务器可以正常启动.

All other similar questions I have found had to do with MultiPart Data and file uploading, whereas I am making a simple GET request. Other functions that also use the javax.ws.rs.code.Response class do not have this issue and the server starts normally.

我已经注意到,只要OffsetDateTime类位于参数中(即param3param4),就会发生此问题,但是我一直无法找出原因.而且,OffsetDateTime是Swagger Codegen选择的,我不愿更改它,因为看到以后每当我重新生成源代码时,我将必须如何更改每个派生文件.

I have noticed that the problem happens whenever the OffsetDateTime class is in the parameters (i.e. param3 and param4), but I have been unable to find out why. Moreover, OffsetDateTime was chosen by Swagger Codegen and I am reluctant to change it seeing how I will have to change every derived file afterwards whenever I regenerate my sources.

有人在使用REST服务和OffsetDateTime之前遇到过此问题吗?

Has anyone had this issue before with REST services and OffsetDateTime?

推荐答案

我发现的所有其他类似问题都与MultiPart数据和文件上传有关

All other similar questions I have found had to do with MultiPart Data and file uploading

有关.该错误是当Jersey无法验证资源模型时遇到的一般错误.资源模型的一部分是方法参数.泽西岛拥有一个系统,该系统知道它将能够处理哪些参数,而不会处理哪些参数.就您而言,它不知道如何处理OffsetDateTime.

It's related. The error is a general error you get when Jersey can't validate the resource model. Part of the resource model is the method parameters. Jersey has a system for knowing which parameters it will be able to process and which ones it won't. In your case, it doesn't know how to process the OffsetDateTime.

为了能够将非基本类型用作

There are a set of rules that you need to follow in order to able to use non basic types as @QueryParams (and all other @XxxParams such as @PathParam and @FormParam, etc.):

  1. 成为原始类型
  2. 具有一个接受单个String参数的构造函数
  3. 具有名为valueOffromString的静态方法,该方法接受单个String参数(例如,参见Integer.valueOf(String))
  4. 具有ParamConverterProvider JAX-RS扩展SPI的注册实现,该实现返回一个ParamConverter实例,该实例能够对该类型进行从字符串"转换.
  5. List<T>Set<T>SortedSet<T>,其中T满足以上2、3或4.结果集合是只读的.
  1. Be a primitive type
  2. Have a constructor that accepts a single String argument
  3. Have a static method named valueOf or fromString that accepts a single String argument (see, for example, Integer.valueOf(String))
  4. Have a registered implementation of ParamConverterProvider JAX-RS extension SPI that returns a ParamConverter instance capable of a "from string" conversion for the type.
  5. Be List<T>, Set<T> or SortedSet<T>, where T satisfies 2, 3 or 4 above. The resulting collection is read-only.

因此,在OffsetDateTime的情况下,进入列表;这不是原始的;它没有String构造函数;它没有静态的valueOffromString

So in this case of OffsetDateTime, going down the list; it's not a primitive; it doesn't have a String constructor; it doesn't have a static valueOf or fromString

因此,基本上,剩下的唯一选择就是为其实现ParamConverter/ParamConverterProvider.基本设置看起来像

So basically, the only option left is to implement a ParamConverter/ParamConverterProvider for it. The basic set up looks like

@Provider
public class OffsetDateTimeProvider implements ParamConverterProvider {

    @Override
    public <T> ParamConverter<T> getConverter(Class<T> clazz, Type type, Annotation[] annotations) {
        if (clazz.getName().equals(OffsetDateTime.class.getName())) {

            return new ParamConverter<T>() {

                @SuppressWarnings("unchecked")
                @Override
                public T fromString(String value) {
                    OffsetDateTime time = ...
                    return (T) time;
                }

                @Override
                public String toString(T time) {
                    return ...;
                }
            };
        }
        return null;
    }
}

Jersey将为您传递查询参数的String值,创建并返回它是您的工作.

Jersey will pass you the String value of the query parameter, and it's your job to to create it and return it.

然后只需在应用程序中注册OffsetDateTimeProvider.如果您使用的是包扫描,则应从@Provider批注中自动拾取并注册它.

Then just register the OffsetDateTimeProvider with the application. If you're using package scanning, it should be picked up and registered automatically from the @Provider annotation.

我不使用Swagger,所以我不知道他们是否已经提供了已经实现的功能,但是奇怪的是,他们会为您生成此功能,而没有办法使其工作.我知道Jersey 3将提供Java 8支持,但是谁知道什么时候发布.

I don't use Swagger, so I don't know if they already provide something like this already implemented, but it seems odd that they would generate this for you, and not have a way to make it work. I know Jersey 3 will have Java 8 support out the box, but who know when the heck that's gonna be released.

这篇关于OffsetDateTime产生“未找到类型为public javax.ws.rs.core.response的参数的注入源".在GET方法中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆