Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
OffsetDateTime收益率“;找不到public javax.ws.rs.core.response类型参数的注入源;in-GET方法_Java_Rest_Jax Rs_Swagger - Fatal编程技术网

OffsetDateTime收益率“;找不到public javax.ws.rs.core.response类型参数的注入源;in-GET方法

OffsetDateTime收益率“;找不到public javax.ws.rs.core.response类型参数的注入源;in-GET方法,java,rest,jax-rs,swagger,Java,Rest,Jax Rs,Swagger,我有以下GETREST方法: 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

我有以下
GET
REST方法:

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
Transaction
model类一样,是使用Swagger-Codegen生成的实现。我在这个类中还有几个其他函数,但是每当我不注释
GET/transactions
函数时,我都会收到以下错误:

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
类的函数没有这个问题,服务器正常启动

我注意到只要
OffsetDateTime
类在参数中(即
param3
param4
),问题就会发生,但我无法找出原因。此外,
OffsetDateTime
是由Swagger Codegen选择的,我不愿意更改它,因为我以后每次重新生成源代码时都必须更改每个派生文件

以前是否有人在REST服务和OffsetDateTime中遇到过此问题

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

这是相关的。该错误是Jersey无法验证资源模型时出现的一般错误。资源模型的一部分是方法参数。泽西岛有一个系统,可以知道哪些参数可以处理,哪些参数不能处理。在您的情况下,它不知道如何处理
OffsetDateTime

为了能够使用非基本类型作为s(以及所有其他
@XxxParams
,如
@PathParam
@FormParam
等),您需要遵循一组规则:

  • 是原始类型
  • 拥有一个接受单个
    字符串
    参数的构造函数
  • 具有一个名为
    valueOf
    fromString
    的静态方法,该方法接受单个字符串参数(例如,请参见
    Integer.valueOf(String)
  • 拥有
    ParamConverterProvider
    JAX-RS扩展SPI的注册实现,该扩展SPI返回一个
    ParamConverter
    实例,该实例能够对类型进行“from string”转换
  • Be
    List
    Set
    SortedSet
    ,其中
    T
    满足上述2、3或4条要求。生成的集合是只读的
  • 因此,在这种情况下,
    OffsetDateTime
    ,沿着列表;它不是原始的;它没有字符串构造函数;它没有静态
    fromString

    因此,基本上,剩下的唯一选项是为其实现
    ParamConverter/ParamConverterProvider
    。基本设置如下所示

    @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;
        }
    }
    
    @Provider
    公共类OffsetDateTimeProvider实现ParamConverterProvider{
    @凌驾
    public ParamConverter getConverter(类clazz、类型Type、注释[]注释){
    if(clazz.getName().equals(OffsetDateTime.class.getName())){
    返回新的参数转换器(){
    @抑制警告(“未选中”)
    @凌驾
    公共T fromString(字符串值){
    OffsetDateTime时间=。。。
    返回时间(T);
    }
    @凌驾
    公共字符串到字符串(T时间){
    返回。。。;
    }
    };
    }
    返回null;
    }
    }
    
    Jersey将向您传递查询参数的字符串值,您的任务是创建并返回它

    然后只需向应用程序注册
    OffsetDateTimeProvider
    。如果您正在使用包扫描,则应自动从
    @Provider
    注释中获取并注册包扫描

    我不使用招摇过市,所以我不知道他们是否已经提供了类似的已经实现的东西,但他们会为您生成这些东西,而没有办法使其工作,这似乎很奇怪。我知道Jersey 3将提供对Java8的开箱即用支持,但谁知道它什么时候会发布呢