尽管不遵守JAX-RS规则,但@GET方法中的@ QueryParam / @ PathParam参数仍然有效

时间:2018-09-28 08:06:43

标签: java rest jersey jax-rs jersey-test-framework

我在一个类中定义了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.

在代码库中,MyClass1MyClass2没有遵循上述任何条件,但是在PROD上一切正常。但是,当我尝试使用Jersey测试框架测试相同的方法时,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/@QueryParamsMyClass1的{​​{1}}绑定需要上述条件。我为公共构造函数提供了MyClass2的单个String参数,问题部分消失了。我对MyClass1进行了相同的尝试,但是问题仍然存在。 MyClass2是一个静态内部类,它也具有一个父类。所以这是我的问题:

  1. 如何在PROD上正常工作,但在JUnit测试上抱怨呢?
  2. 对于用于绑定MyClass2的静态内部类,是否需要进行特殊考虑?我为静态内部类提供了一个带有单个String arg的公共构造函数,但问题仍然存在,而其他类(@PathParams/@QueryParams)仍然有效。有什么我想念的吗?

谢谢!

0 个答案:

没有答案