使用GSON时,JerseyTest中会抛出MessageBodyProviderNotFoundException

时间:2017-06-27 11:25:12

标签: java gson jersey-2.0 jersey-test-framework

我使用Jersey并决定使用GSON代替Moxy进行JSON处理(不喜欢Moxy需要setter的事实)。

到目前为止,一切正常,除了JerseyTest子类中的一个非常恼人的问题:除非为每个调用明​​确注册,否则不会识别自定义GsonProvider。但是,如果我将应用程序部署到Tomcat,则会被识别。

我的ResourceConfig

@ApplicationPath("")
public class MyResourceConfig extends ResourceConfig {

    public MyResourceConfig() {
        register(GsonProvider.class);

        register(SomeResource.class);
    }
}

GsonProvider的实施(虽然我认为它与我遇到的问题无关):

@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class GsonProvider<T> implements MessageBodyReader<T>, MessageBodyWriter<T> {

    private final Gson mGson;

    public GsonProvider() {
        mGson = new GsonBuilder().create();
    }

    @Override
    public boolean isReadable(Class<?> type, Type genericType,
                              Annotation[] annotations, MediaType mediaType) {
        return true;
    }

    @Override
    public T readFrom(Class<T> type, Type genericType, Annotation[] annotations,
                      MediaType mediaType, MultivaluedMap<String, String> httpHeaders,
                      InputStream entityStream) throws IOException, WebApplicationException {

        InputStreamReader reader = new InputStreamReader(entityStream, "UTF-8");
        try {
            return mGson.fromJson(reader, type);
        } finally {
            reader.close();
        }
    }

    @Override
    public boolean isWriteable(Class<?> type, Type genericType,
                               Annotation[] annotations, MediaType mediaType) {
        return true;
    }

    @Override
    public long getSize(T t, Class<?> type, Type genericType,
                        Annotation[] annotations, MediaType mediaType) {
        return -1;
    }

    @Override
    public void writeTo(T t, Class<?> type, Type genericType, Annotation[] annotations,
                        MediaType mediaType, MultivaluedMap<String, Object> httpHeaders,
                        OutputStream entityStream) throws IOException, WebApplicationException {

        PrintWriter printWriter = new PrintWriter(entityStream);
        try {
            String json = mGson.toJson(t);
            printWriter.write(json);
            printWriter.flush();
        } finally {
            printWriter.close();
        }
    }
}

此测试结果为MessageBodyProviderNotFoundException

public class SomeResourceTest extends JerseyTest {
    @Override
    public Application configure() {
        return new MyResourceConfig();
    }

    @Test
    public void someApi_200Returned() throws Exception {
        // Arrange
        // Act
        SomeResponse response =
                target("/somepath")
                        .request()
                        .post(Entity.json(""), SomeResponse.class);
        // Assert
        assertThat(response.getStatus(), is(200));
    }
}

为了解决此问题,我请求GsonProvider请求。以下更改使测试通过:

public class SomeResourceTest extends JerseyTest {
    @Override
    public Application configure() {
        return new MyResourceConfig();
    }

    @Test
    public void someApi_200Returned() throws Exception {
        // Arrange
        // Act
        SomeResponse response =
                target("/somepath")
                        .register(GsonProvider.class)
                        .request()
                        .post(Entity.json(""), SomeResponse.class);
        // Assert
        assertThat(response.getStatus(), is(200));
    }
}

因此,GsonProviderMyResourceConfig的注册有助于部署,但JerseyTest需要针对每个请求进行额外注册。

虽然我可以忍受这种情况,但它很烦人,耗时且很难与其他团队成员沟通。针对这个问题的任何解决方案?

1 个答案:

答案 0 :(得分:1)

你没有展示过堆栈跟踪,但我很确定如果仔细观察它,它会显示它实际上是客户端错误。您需要做的是将gson提供程序注册到客户端,因为您尝试将响应JSON反序列化为POJO

@Override
public void configureClient(ClientConfig config) {
    config.register(GsonProvider.class)
}

configureClient方法是您可以覆盖的JerseyTest中的方法。