对于 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 需要可转换。实现此目的的选项:
public static SomeValueDTO valueOf(String param)public static SomeValueDTO fromString(String param)ParamConverter .你可以看一个例子 here 在前三种情况中的任何一种情况下,您都需要通过在构造函数或上述方法之一中解析字符串来相应地构造实例。
通常,您只想将 ParamConverter 用于您无法编辑的第三方类。否则将其他三个选项用于您自己的类(class)。
关于java - Jersey ModelValidationException - 未找到注入(inject)源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29676563/