Spring-Jersey 应用程序的 JerseyTest 中的资源模型验证失败

2023-12-10

我有一个带注释的 Spring-Jersey 应用程序。我正在尝试使用以下命令为我的控制器设置单元测试JerseyTest。我在运行测试时遇到以下错误,但我无法弄清楚。我错过了什么?

SEVERE: Following issues have been detected: 
WARNING: No injection source found for a parameter of type public com.example.services.dto.UnitDto com.example.apis.UnitResource.getUnits(com.example.services.dto.PointDto) throws com.example.exceptions.PointOutsideBounds at index 2.

Validation of the application resource model has failed during application initialization.
[[FATAL] No injection source found for a parameter of type public com.example.services.dto.UnitDto com.example.apis.UnitResource.getUnits(com.example.services.dto.PointDto) throws com.example.exceptions.PointOutsideBounds at index 2.; source='ResourceMethod{httpMethod=GET, consumedTypes=[], producedTypes=[application/json], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class com.example.apis.UnitResource, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@31a5870e]}, definitionMethod=public com.example.services.dto.UnitDto com.example.apis.UnitResource.getUnits(com.example.services.dto.PointDto) throws com.example.exceptions.PointOutsideBounds, parameters=[Parameter [type=class com.example.services.dto.PointDto, source=contains, defaultValue=null]], responseType=class com.example.services.dto.UnitDto}, nameBindings=[]}']
    at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:555)

控制器类如下:

@Component
@Path("/app/units")
public class UnitResource {
    @Context
    private UriInfo uriInfo;
    @Context
    private Request request;

    @Inject
    private UnitService unitService;

    @Inject
    public UnitResource(@Named("UnitService") UnitService unitService) {
        this.unitService = unitService;
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Timed(absolute = true, name = "getUnitForPoint")
    public UnitDto getUnits(@QueryParam("contains") @NotNull PointDto point) throws PointOutsideBounds {
        return unitService.getUnitForPoint(point);
    }
}

测试类如下:

public class UnitResourceTest extends JerseyTest {

    @Mock
    private UnitService unitService;

    @Override
    protected ResourceConfig configure() {
        ResourceConfig rc = new ResourceConfig()
        //      .register(new UnitResource(Mockito.mock(UnitService.class))) -- has the same effect
                .register(UnitResource.class)
                .property("contextConfig", new AnnotationConfigApplicationContext(ApplicationConfiguration.class));

        enable(TestProperties.LOG_TRAFFIC);
        enable(TestProperties.DUMP_ENTITY);
        forceSet(TestProperties.CONTAINER_PORT, "0");

        return rc;
    }

    @Before
    public void setUp() throws Exception {
        super.setUp();
        initMocks(this);
    }

    @Override
    protected TestContainerFactory getTestContainerFactory() {
        return new InMemoryTestContainerFactory();
    }

    @Test
    public void testGetUnit() throws PointOutsideBounds {

        Response response = target()
                .path("/app/units")
                .queryParam("contains", new Point(0.0,0.0).toJson())
                .request(MediaType.APPLICATION_JSON_TYPE)
                .get();

        assertThat(response.getStatus(), is(Response.Status.OK));
    }
}

应用程序配置类是:

@Configuration
@ComponentScan("com.example")
public class ApplicationConfiguration {
    @Bean(name="UnitService")
    public UnitService unitService() {
        return new UnitService();
    }
}

我的 Gradle 测试配置依赖项是:

dependencies {
    // Using junit-dep package to get junit without hamcrest dependency
    testCompile("junit:junit-dep:4.8.2")
    testCompile("org.hamcrest:hamcrest-all:1.3")
    testCompile("org.mockito:mockito-all:1.8.4")
    testCompile ('org.glassfish.jersey.test-framework:jersey-test-framework-core:2.22.2')
    testCompile('org.glassfish.jersey.test-framework.providers:jersey-test-framework-provider-inmemory:2.22.2')
}

configurations {
    testCompile.exclude group: 'org.glassfish.jersey.ext', module: 'jersey-spring3'
}

I found this and this但都没有解决我的问题。


Note:在大多数情况下,这个答案适用于所有@XxxParam带注释的参数。

The PointDto参数似乎是问题

public UnitDto getUnits(@QueryParam("contains") @NotNull PointDto point)

查询参数是字符串。 Jersey 能够将该字符串转换为一些基本类型,例如int, boolean, Date等等,但它不知道如何创建PointDto从那个字符串。这并不意味着我们不能使用自定义类型。我们只需要遵循一些规则:

  1. 有一个接受字符串的构造函数。然后我们需要根据该字符串自己构造对象。例如

    public PointDto(String param) {
        Map<String, Object> map = parseJson(param);
        this.field1 = map.get("field1");
        this.field2 = map.get("field2");
    }
    

    这有点工作量。

  2. 另一种选择是静态fromString(String)或静态valueOf(String)在课堂里。 Jersey 将调用此方法来构造对象。例如你可以在你的PointDto class

    public static PointDto fromString(String param) {
        PointDto dto = parseJson(param);
        return dto;
    }
    
  3. 你可以有一个ParamConverterProvider。我不会举例,但您可以阅读更多内容这篇好文章关于如何实施它。您还可以看到这个帖子

请注意,此答案中提供的信息位于 javadoc 中@QueryParam.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring-Jersey 应用程序的 JerseyTest 中的资源模型验证失败 的相关文章

随机推荐

  • 无法在 AutoMapper 5 中从 ViewModel 映射到 ApplicationUser

    我有一个从 ApplicationUser 基类 ASP NET Identity 继承的 Student 类 它有一个名为 StudentViewModel 的 ViewModel 如下所示 实体类 public class Applic
  • Google Play 说我的 apk 使用两个本机平台(我的库),因此它支持 0 台设备

    当我在 Google Play 上发布 apk 时 它显示 支持 0 台设备 在 apk 描述的底部写着 本机平台 actionbarsherlock SherlockNavigatorDrawer 这些是我用于我的项目的库 它们不是 ja
  • 在 C++ 中模拟 lambda 的复制赋值运算符

    这个问题有两个部分 Firstly 有人可以解释 C 禁用 lambda 的复制赋值运算符背后的基本原理吗 如果您要允许复制构造函数 为什么不允许复制赋值运算符呢 Secondly 如何在不强迫人们编写 C 03 风格函子或使用 std f
  • MediaElement 和 RTSP

    MediaElement 支持 RTSP 吗 我有一个流正在运行 我可以通过以下方式使用 VLC 播放器使用该流 rtsp 192 168 1 17 554 stream 但是 当尝试使用 Windows Media Player 时 我无
  • 如何将一个巨大的矩阵逐行写入文件(fortran 90)

    我想将一个包含大量数据的矩阵逐行写入文件中 例如 我有一个 100 100 的矩阵 我想在文件中以 100 100 的形式保存它 但是 它不起作用 以下是我的代码和一些描述 N和M是数百左右的整数 RECL 是预期长度我设置了文件 但在这里
  • 如何通过行列切换进行矩阵转换?

    我有一个由元素组成的方阵 1 或 0 第 i 行切换会切换所有第 i 行元素 1 变为 0 反之亦然 并且第 j 列切换切换所有 第 j 列元素 我有另一个方阵 大小相似 我想将初始矩阵更改为 使用最少切换次数的最终矩阵 例如 0 0 1
  • 使用清单的 DLL 重定向

    我需要可靠地重定向应用程序对特定 DLL 的查找 使用 app exe local 方法不起作用 因为如果应用程序具有清单 嵌入或单独的文件 则本地文件将被忽略 因此 我尝试通过在清单中将 DLL 定义为私有程序集来进行 DLL 重定向 我
  • 线性时间排序? [关闭]

    Closed 这个问题不符合堆栈溢出指南 目前不接受答案 给定 0 n 3 1 范围内的 n 个整数的输入集 提供线性时间排序算法 这是我周四测试的回顾 我不知道如何解决这个问题 也看看相关的排序 鸽巢排序 or 计数排序 也基数排序正如普
  • 从 VSTO PowerPoint 功能区调用 VBA AddIn 宏

    我在这个问题上被困了几个小时 我正在用 C 开发一个 PowerPoint 插件 我想使用另一个插件 PPAM 文件 中的宏 PPAM 文件已安装并启用 在应用程序参考中我发现我需要使用Application Run方法 但我无法让它工作
  • Selenium——如何等待页面完全加载[重复]

    这个问题在这里已经有答案了 我正在尝试使用 Java 和 Selenium WebDriver 自动化一些测试用例 我有以下场景 有一个名为 产品 的页面 当我点击 查看详细信息 链接时 在 产品 页面中 会出现一个包含商品详细信息的弹出窗
  • 当组合根位于客户端时如何在 WCF 中注入依赖项

    在开始之前 我必须说 我可能咬得太多 但我正处于绝望的学习狂潮中 我需要很多帮助 我正在编写一个练习 从两本书中获取样本 1 Net 中的依赖注入 作者 Mark Seemann2 Brian Egan 和 Steve Valenzuela
  • React-router transitionTo 不是一个函数

    import React from react import Router Link Navigation from react router export default class ResourceCard extends React
  • 子图之间的箭头

    我决定玩玩this示例代码一点 我能够弄清楚如何在两个子图之间绘制一条直线 即使该线位于其中一个子图的边界之外 import matplotlib pyplot as plt import matplotlib as mpl import
  • 在 Python 中通过管道传输到脚本时无法启动交互式程序

    我有一个 python 脚本需要调用定义的 EDITOR or VISUAL 当单独调用 Python 脚本时 我可以启动 EDITOR没有任何问题 但是当我将某些内容传输到 Python 脚本时 EDITOR无法启动 现在 我正在使用 n
  • 在expect中使用argc和argv解析命令行

    我有一个期望例程 它需要生成一个进程并将我传递给期望例程的命令行参数传递给生成的进程 我的期望例程有以下几行 spawn myProcess argv 当我调用我的期望例程时 我从命令行调用它 如下所示 expect myRoutine
  • 停止并重新启动 VGG-16 上的训练

    我正在使用预训练的 VGG 16 模型进行图像分类 我添加了自定义最后一层 因为我的分类类数量为 10 我正在对模型进行 200 轮训练 我的问题是 如果我随机停止 通过关闭 python 窗口 在某个时期的训练 有什么办法吗 假设时期没有
  • SSRS:我可以知道用户是否在多值参数中选择了“ALL”吗?

    客户希望我重复报告页眉中的参数值 但是 如果他们只是在多值参数上选择 全选 则他们希望列出文本 任意 例如 一个参数具有一组固定的 9 个值 我将文本框的表达式硬编码为 Room Size iif Parameters pRoomCap C
  • Python Numpy Loadtxt - 转换 unix 时间戳

    我有一个包含许多行数据的文本文件 每行中的第一条数据是一个 unix 时间戳 例如1436472000 我在用numpy loadtxt在转换器的参数中 我想指定它将时间戳转换为 numpy 理解的日期时间 我知道这需要在0 在大括号中 但
  • 将运算符作为参数传递给 C 中的函数

    我想将大于 gt 和小于 你可以用宏做一些可怕的事情 但一般来说 不 你不能这样做 您通常接受两个参数函数并调用它 并且该函数可以使用 gt or lt 酌情参见the sort docs举个例子 也就是说 它不是超级高效 通过指针调用函数
  • Spring-Jersey 应用程序的 JerseyTest 中的资源模型验证失败

    我有一个带注释的 Spring Jersey 应用程序 我正在尝试使用以下命令为我的控制器设置单元测试JerseyTest 我在运行测试时遇到以下错误 但我无法弄清楚 我错过了什么 SEVERE Following issues have