mercredi 2 mai 2018

JacksonJaxbJsonProvider cannot read json for JerseyTest, throwing nullPointerException

My Api function runs OK from Swagger. But I wanted to automate its tests and have tried JerseyTest for it. It looks excellent, but I cannot make it to take json strings, as the API function takes.

When I am trying to send the minimal json string as request body, as here:

    Entity<String> entity = Entity.json("{\"root\":\"true\"}");
    WebTarget webTarget = target(targetUrl);
    try (Response response = webTarget.request().method("POST", entity)) {

I am getting the exception:

javax.ws.rs.ProcessingException: Server-side request processing failed with an error.

With cause:

java.lang.NullPointerException

My settings for JerseyTest are:

    @Override
    protected Application configure() {
        if (null == applicationRuntime) {
            applicationRuntime = new ResourceConfig(DocumentTypes.class, JacksonJaxbJsonProvider.class);
        }
        return applicationRuntime;
    }

    @Override
    protected void configureClient(final ClientConfig config) {
        config.register(JacksonJaxbJsonProvider.class);
    }

The header of the API function:

@POST
@Consumes(value = { MediaType.APPLICATION_JSON + ";charset=utf-8" })
@Produces(value = { MediaType.APPLICATION_JSON + ";charset=utf-8" })
@ApiOperation(value = "Create a document type", notes = "creates a document type from Json and returns the created type", response = Response.class)

@PublicApi
public Response create(
        @ApiParam(example = DOC_TYPE_EXAMPLE, value = "Created DocumentType", required = true) @RequestBody com.infor.daf.icp.internal.rest.models.DocumentType body) {

The imports in the test:

import javax.ws.rs.ProcessingException;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response;

import org.apache.commons.io.IOUtils;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;

// import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider; // I tried this lib, too.
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import com.infor.daf.icp.internal.json.JSONException;
import com.infor.daf.icp.internal.json.JSONObject;

Aucun commentaire:

Enregistrer un commentaire