简体   繁体   中英

Java: Wrong JSON schema when using javax.json.JsonObject and the RESTEasy embedded server

I am writing tests using TJWSEmbeddedJaxrsServer. I got the JSON serialization working but the format is different from when I deploy the code on an WildFly 10 application server.

The test JSON is generated via javax.json.Json. If I just serialize an object I don't have this problem.

@GET
@Path("/testgroup")
@Produces(MediaType.APPLICATION_JSON)
public Response getTestGroup() String filter) {

    JsonObject jsonObject = Json.createObjectBuilder()
                 .add("id", 1L)
                 .add("label", "TestGroup1").build();

    return Response.ok(jsonObject).build();
}

The JSON from the TJWSEmbeddedJaxrsServer :

{
    "id": {
        "integral": true,
        "valueType": "NUMBER"
    },
    "label": {
        "chars": "TestGroup1",
        "string": "TestGroup1",
        "valueType": "STRING"
    }
}

The JSON from the same code deployed on the WildFly :

{
     id: 1,
     label: "TestGroup1"
}

The Maven dependencies:

<dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc7</artifactId>
            <version>12.1.0.1.0-AP</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jackson-provider</artifactId>
            <version>${resteasy.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.json</groupId>
            <artifactId>javax.json-api</artifactId>
            <version>1.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.json</artifactId>
            <version>1.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxrs</artifactId>
            <version>${resteasy.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-client</artifactId>
            <version>${resteasy.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>3.1.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

Test Class:

@BeforeClass
public static void init() {
    scaleRecource = new ScaleRecource();
    scaleRecource.masterDataService = new MasterDataService();

    TJWSEmbeddedJaxrsServer server = new TJWSEmbeddedJaxrsServer();
    server.setPort(1234);
    server.getDeployment().getResources().add(scaleRecource);
    server.start();

    RestAssured.port=1234;
}

@Test
public void testGetGroup() {

    given()
      .when().get(rootPath + "/testgroup")
      .then().log().body().statusCode(200);

}

Are the Maven dependencies wrong or how do I configure the serialization with the RESTeasy embedded server?

Edit : If I remove the org.glassfish.javax.json dependency. I do get an error that the class org.glassfish.json.JsonProviderImpl can not be found. But a serialization without using the the JsonObject does still work:

return Response.ok(new CodedEntry("code1", "testgroup")).build();

Also this type of serialization does result in a expected JSON even when using the TJWSEmbeddedJaxrsServer.

Is there another implementation for the javax.json.JsonObject interface present on the WildFly?

As I suspeced the problem was a wrong dependency.

I had to replace the dependency

<dependency>
     <groupId>org.glassfish</groupId>
     <artifactId>javax.json</artifactId>
     <version>1.1</version>
     <scope>test</scope>
</dependency>

with

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-json-p-provider</artifactId>
    <version>${resteasy.version}</version>
</dependency>

As this has the json-p implementation that is bundled with WildFly: https://docs.jboss.org/resteasy/docs/3.0.7.Final/userguide/html/json-p.html

This is only needed for the implementation of the javax.json.JsonObject interface from the Java EE 7 JSON-P API .

I had initially added the wrong one because I got the error that the class org.glassfish.json.JsonProviderImpl can not be found.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM