我在一个类中定义了 JAX-RS API 端点。我在课堂上有方法,例如
@GET
@Path("{param1}/{param2}/mypath")
@Consumes({ MediaType.APPLICATION_XML, MediaType.TEXT_XML,
MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.TEXT_XML,
MediaType.APPLICATION_JSON })
public Response getSomeResource(@PathParam("param1") MyClass1 myclass1,
@PathParam("param2") MyClass2 myclass2 )
{
//business logic goes here
}
现在,根据https://docs.oracle.com/cd/E19776-01/820-4867/6nga7f5n6/index.html上的文档,@QueryParam 和 @PathParam 只能用于以下 Java 类型:
All primitive types except char
All wrapper classes of primitive types except Character
Have a constructor that accepts a single String argument
Any class with the static method named valueOf(String) that accepts
a single String argument
Any class with a constructor that takes a single String as a
parameter
List<T>, Set<T>, or SortedSet<T>, where T matches the already listed
criteria. Sometimes parameters may contain more than one value for
the same name. If this is the case, these types may be used to
obtain all values.
在代码库中,MyClass1
不要MyClass2
遵循上述任何一项,但在 PROD 上一切正常。但是当我尝试使用 Jersey Test 框架测试相同的方法时,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
@PathParams/@QueryParams
在进一步挖掘中,我发现我的绑定需要上述条件MyClass1
和MyClass2
。我提供了一个带有单个 String 参数的公共构造函数,MyClass1
问题部分消失了。我尝试了同样的方法,MyClass2
但问题仍然存在。MyClass2
是一个静态内部类,它也有一个父类。所以这是我的问题:
- 它如何在 PROD 上正常工作但在 JUnit 测试中抱怨?
- 对于用于绑定的静态内部类是否有任何特殊考虑
@PathParams/@QueryParams
?我给静态内部类提供了一个带有单个 String arg 的公共构造函数,但问题仍然存在,而另一个类 (MyClass1
) 它工作。有什么我想念的吗?
谢谢!