英文:
Response.readEntity(String.class) failing to read the response
问题
最近,我突然遇到了一个关于读取实体 API 的问题。我已经附上了 pom.xml 文件以及所使用的代码,能否请有人帮我找出问题所在,代码是用来读取响应的:
Response serverResponse = executeRequest(httpPost, log);
String responseData = serverResponse.readEntity(String.class);
我遇到的错误是:
java.lang.IllegalStateException: Method not supported on an outbound message.
at org.glassfish.jersey.message.internal.OutboundJaxrsResponse.readEntity(OutboundJaxrsResponse.java:144)
pom.xml 文件中的依赖项如下:
<dependencies>
<!-- https://mvnrepository.com/artifact/com.ibm.mfp/adapter-maven-api -->
<dependency>
<groupId>com.ibm.mfp</groupId>
<artifactId>adapter-maven-api</artifactId>
<version>8.0.2018071507</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.4.RELEASE</version>
<type>jar</type>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.ibm.mfp/mfp-java-token-validator -->
<dependency>
<groupId>com.ibm.mfp</groupId>
<artifactId>mfp-java-token-validator</artifactId>
<version>8.0.2017020112</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.ibm.mfp/mfp-security-checks-base -->
<dependency>
<groupId>com.ibm.mfp</groupId>
<artifactId>mfp-security-checks-base</artifactId>
<version>8.0.2018030404</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>
错误消息如下:
nested exception is java.lang.AbstractMethodError: javax/ws/rs/core/Response.readEntity(Ljava/lang/Class;)Ljava/lang/Object;, status=500
英文:
Recently I started facing an issue all of a sudden for read entity API.
I have attached the pom.xml and the code used could someone please help me find the issue here,
code that is reading the response
Response serverResponse = executeRequest(httpPost, log);
String responseData = serverResponse.readEntity(String.class);
Error that I was getting is
java.lang.IllegalStateException: Method not supported on an outbound message.
at org.glassfish.jersey.message.internal.OutboundJaxrsResponse.readEntity(OutboundJaxrsResponse.java:144)
pom.xml
<dependencies>
<!-- https://mvnrepository.com/artifact/com.ibm.mfp/adapter-maven-api -->
<dependency>
<groupId>com.ibm.mfp</groupId>
<artifactId>adapter-maven-api</artifactId>
<version>8.0.2018071507</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.4.RELEASE</version>
<type>jar</type>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.ibm.mfp/mfp-java-token-validator -->
<dependency>
<groupId>com.ibm.mfp</groupId>
<artifactId>mfp-java-token-validator</artifactId>
<version>8.0.2017020112</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.ibm.mfp/mfp-security-checks-base -->
<dependency>
<groupId>com.ibm.mfp</groupId>
<artifactId>mfp-security-checks-base</artifactId>
<version>8.0.2018030404</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>
error message:
nested exception is java.lang.AbstractMethodError: javax/ws/rs/core/Response.readEntity(Ljava/lang/Class;)Ljava/lang/Object;, status=500
答案1
得分: 2
我通过使用以下代码解决了这个问题:
String responseData = serverResponse.getEntity().toString();
而不是:
String responseData = serverResponse.readEntity(String.class);
这与我们构造响应的方式有关。我之前是这样操作响应的:
Response.ResponseBuilder responseBuilder = Response
.status(serverResponse.getStatusLine().getStatusCode())
.entity(responseData != null && !responseData.isEmpty() ? responseData : "{}");
然后当我将 readEntity
更改为 getEntity
时,对我来说问题解决了。
英文:
I fixed this problem by using
String responseData = serverResponse.getEntity().toString();
instead of
String responseData = serverResponse.readEntity(String.class);
It has something to do with the way we form the response. I was manipulating response as
Response.ResponseBuilder responseBuilder = Response
.status(serverResponse.getStatusLine().getStatusCode())
.entity(responseData != null && !responseData.isEmpty() ? responseData : "{}");
and then I was using String responseData = serverResponse.readEntity(String.class);
when I changed readEntity to getEntity it worked fine for me.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论