使用 Jersey 2.6,我遇到了一个我绝对不明白的怪异问题。

我无法解释原因,但是之一查询参数使 Jersey 抛出 ModelValidationException

    @ApiOperation("Save")
    @PUT
    public Response save(
            @HeaderParam("token") final String token,
            @QueryParam("someValue") final SomeValueDTO someValue,
            @QueryParam("anotherParam") final int anotherParam) throws TechnicalException {

        return Response.ok().build();
    }

queryParam'someValue'使 Jersey 抛出:
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 ch.rodano.studies.api.resources.PagesResource.save(java.lang.String,ch.rodano.studies.api.dto.JSONValueDTO,int) throws ch.rodano.studies.exceptions.RightException,ch.rodano.studies.configuration.exceptions.NoNodeException at index 1.; source='ResourceMethod{httpMethod=PUT, consumedTypes=[], producedTypes=[application/json], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class ch.rodano.studies.api.resources.PagesResource, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@41ed3918]}, definitionMethod=public javax.ws.rs.core.Response ch.rodano.studies.api.resources.PagesResource.save(java.lang.String,ch.rodano.studies.api.dto.JSONValueDTO,int) throws ch.rodano.studies.exceptions.RightException,ch.rodano.studies.configuration.exceptions.NoNodeException, parameters=[Parameter [type=class java.lang.String, source=token, defaultValue=null], Parameter [type=class ch.rodano.studies.api.dto.JSONValueDTO, source=valuesASD, defaultValue=null], Parameter [type=int, source=visitPk, defaultValue=null]], responseType=class javax.ws.rs.core.Response}, nameBindings=[]}']

如果我使用String而不是SomeValueDTO,则一切正常。
SomeValueDTO是一个非常经典的POJO,具有一个空的构造函数和getter/setter方法。

如果有人有白痴!

最佳答案

SomeValueDTO需要可转换。实现此目的的选项:

  • 返回类型(SomeValueDTO)的public static SomeValueDTO valueOf(String param)
  • 返回类型(SomeValueDTO)的public static SomeValueDTO fromString(String param)
  • 或接受字符串
  • 的公共(public)构造函数
  • 实现 ParamConverter 。您可以看到一个示例here

  • 在前三种情况中,您都希望通过在构造函数或上述方法之一中解析String来相应地构造实例。
    通常,您只想将ParamConverter用于无法编辑的第三方类。否则,对您自己的类(class)使用其他三个选项。

    10-05 19:06