我正在制作示例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.如果我返回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 as a member variable in POJO not recognized -Jersey完全不同,因为:
保罗在上述问题中的回答与这个问题更为相关。至少,它是关于JerseyTest。但是答案中的想法不能在这里使用。建议的依赖关系使得REST调用在500而不是400上失败。register(JacksonFeature.class)
根本不改变行为。并且建议的映射器必须获取映射类型的类。这正是我想要逃避的。无论如何,即使它是适用的,答案的接近并不意味着问题的重复。