小编Nie*_*els的帖子

JAX-RS(Reasteasy)Response.readEntity抛出:IllegalStateException:RESTEASY003290:实体不受输入流支持

我有一个发送JAX-RS POST调用的方法的JUnit测试.为了独立于外部资源,我模拟了REST客户端,并说应该返回一个虚拟响应.效果很好,没问题.但:

在调用时myResponse.readEntity(String.class)我总是得到以下异常:

java.lang.IllegalStateException:RESTEASY003290:实体不受输入流支持

这是我的代码片段失败:

import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.Test;

public class SimpleTest {

    @Test
    public void testReadResponse() {
        final JsonObject responseContent = new JsonObject();
        responseContent.add("field", new JsonPrimitive("This is a JSON for testing."));
        final String expected = responseContent.toString();
        final Response.ResponseBuilder builder = Response.ok()
            .entity(responseContent.toString())
            .header("Content-Type", MediaType.APPLICATION_JSON);
        final Response dummyResponse = builder.build();
        final String result = dummyResponse.readEntity(String.class); // <-- Exception is thrown here!
        assertThat("JSON Strings are not …
Run Code Online (Sandbox Code Playgroud)

java rest junit jax-rs mocking

6
推荐指数
1
解决办法
8727
查看次数

标签 统计

java ×1

jax-rs ×1

junit ×1

mocking ×1

rest ×1