首页 > 解决方案 > 我可以将响应实体作为 JSONObject 传递,而不是创建一个字符串吗?

问题描述

我正在为它们制作示例 Rest 函数和测试。

其余函数看起来是这样的:

@Path("ftoc/")
public class FtoC {
  @Path("{f}")
  @GET
  @Produces("application/json")
  public Response convertFtoCfromInput(@PathParam("f") double f) throws JSONException {

    JSONObject jsonObject = new JSONObject();
    double celsius;
    celsius =  convert(f); 
    jsonObject.put("F Value", f); 
    jsonObject.put("C Value", celsius);

    TemperatureCF t = new TemperatureCF(celsius, f);

    //return Response.status(200).entity(jsonObject.toString()).build(); 
    return Response.status(200).entity(jsonObject).build();
    //return Response.status(200).entity(t).build();

  }

我试图以三种方式传递响应——作为 POJO、作为字符串和作为 JSONObject。

读取结果的测试函数是:

@Test
public void massConvertFtoC() {
  Response response = target("/ftoc/0").request().get();
  assertEquals(response.getStatus(), 200); // here it fails, 400 instead of 200
....
}


@Override
protected Application configure() {
  return new ResourceConfig(FtoC.class);
}

@BeforeClass
public void before() throws Exception {
    super.setUp();
}

String 和 POJO 变体都可以通过测试启动并返回有序状态 200。如果我 return JSONObject,当我检查该return Response.status(200).entity(jsonObject).build()行时,它也会创建正确的响应。但是当进程到达标记线时,它会失败:响应状态为 400。如果我readEntity(String.class),我得到:

没有为类 org.json.JSONObject 找到序列化程序,也没有发现创建 BeanSerializer 的属性(为避免异常,请禁用 SerializationFeature.FAIL_ON_EMPTY_BEANS)

我的 Maven 依赖项是:

<dependencies>
    <dependency>
        <groupId>asm</groupId>
        <artifactId>asm</artifactId>
        <version>3.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20170516</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.20</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.14.3</version>
    </dependency>

    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>${jaxrs.version}</version>
    </dependency>

    <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.0.1</version>
          <scope>provided</scope>
    </dependency> 

    <!-- Jersey 2.27 -->
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>${jersey2.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>${jersey2.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.inject</groupId>
        <artifactId>jersey-hk2</artifactId>
        <version>${jersey2.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-common</artifactId>
        <version>${jersey2.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>${jersey2.version}</version>
    </dependency>       
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <version>${jersey2.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.test-framework.providers</groupId>
        <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
        <version>${jersey2.version}</version>
    </dependency>
</dependencies>

我正在寻找一个简单的解决方案。添加 .register(JacksonFeature.class) - 没关系(真的没有帮助)。但我不想创建一些大型的附加类。我


编辑关于假定的重复:
这个问题与JSONObject 作为 POJO 中的成员变量不被识别 -Jersey完全不同,因为:

保罗对上述问题的回答与这个问题更相关。至少,它是关于 JerseyTest 的。但是答案中的想法不能在这里使用。建议的依赖项使 REST 调用在 500 而不是 400 上失败。register(JacksonFeature.class)根本不要改变行为。并且建议的映射器必须获取映射类型的类。这正是我想要逃避的。无论如何,即使它适用,答案的接近并不意味着问题的重复。

标签: javarestjakarta-eejerseyjersey-test-framework

解决方案


推荐阅读