1

I am making example Rest functions and tests for them.

The rest function looks so:

@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();

  }

I am trying to pass the response in three way - as a POJO, as a string and as a JSONObject.

The test function that reads the result, is:

@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();
}

The String and POJO variants both can be launched by the test and return the ordered status 200. If I return JSONObject, when I inspect the return Response.status(200).entity(jsonObject).build() line, it creates correct response, too. But when the process reaches the marked line, it fails: the response is with status 400 instead. If I readEntity(String.class), I am getting:

No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)

My maven dependencies are:

<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>

I am looking for a simple solution. To add .register(JacksonFeature.class) - that is OK (does not help really). But I don't want to create some large additional classes. I


Edit about supposed duplication:
The question is absolutely different from JSONObject as a member variable in POJO not recognized -Jersey, because:

  • THERE the problem is not working JSONObject in POJO, and HERE the problem is that JSONObject won't work and POJO works. And JSONObject is not in POJO.
  • The problem message is not mentioned THERE.
  • THERE the question is not about testing and/or JerseyTest

The Paul's answer from the mentioned question is more connected to this question. At least, it is about JerseyTest. But ideas from the answer cannot be used here. The proposed dependency makes the REST call fail on 500 instead of 400. register(JacksonFeature.class) don't change the behaviour at all. And proposed mapper must get the class of the mapped type. And that is EXACTLY what I want to escape. And anyway, even if it were applicable, closeness of the answer does not mean duplication of the question.

Gangnus
  • 24,044
  • 16
  • 90
  • 149
  • can you please show the `@Path` annotation from the class? Because you call `target("/ftoc/0")` but you listen on your method only on `@Path("{f}")` – Lino Jun 04 '18 at 12:53
  • @Lino Thank you, but the problem, obviously, is not there - two other "return" work. And incorrect path will cause 404, not 400. – Gangnus Jun 04 '18 at 12:57
  • The solution in the duplicate is the solution for your problem. Please read the entire answer. You need to use the context resolver for the configured ObjectMapper. – Paul Samsotha Jun 04 '18 at 23:01
  • Also read the **Note** about making sure you use the right dependency version. It will be different for you than what is in the answer – Paul Samsotha Jun 04 '18 at 23:20
  • @PaulSamsotha 1. Marking the duplication is for questions is NOT for marking questions that are in some way answered in answers to other questions. You should use a comment with reference for that case. You mistook here. 2. You have absolutely no rights to consider your (or somebody else's) answer as the true one and MAKE the questioner to use it and block other answers. You cannot even merely up/downvote your answer. You misused your privileges here. – Gangnus Jun 05 '18 at 04:39
  • @PaulSamsotha Your answer is not applicable to my question at all. Your dependencies are not correct. They have not JsonMerge class. And you have blocked the possibility to write here better, usable answers. Your Note? What note? – Gangnus Jun 05 '18 at 14:14
  • @PaulSamsotha Add https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations dependency. After that it works. – Gangnus Jun 05 '18 at 14:17

0 Answers0