Response.readEntity(String.class) 无法读取响应。

huangapple go评论79阅读模式
英文:

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

 &lt;dependencies&gt;
&lt;!-- https://mvnrepository.com/artifact/com.ibm.mfp/adapter-maven-api --&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.ibm.mfp&lt;/groupId&gt;
&lt;artifactId&gt;adapter-maven-api&lt;/artifactId&gt;
&lt;version&gt;8.0.2018071507&lt;/version&gt;
&lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/org.springframework/spring-web --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework&lt;/groupId&gt;
&lt;artifactId&gt;spring-web&lt;/artifactId&gt;
&lt;version&gt;3.0.4.RELEASE&lt;/version&gt;
&lt;type&gt;jar&lt;/type&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/log4j/log4j --&gt;
&lt;dependency&gt;
&lt;groupId&gt;log4j&lt;/groupId&gt;
&lt;artifactId&gt;log4j&lt;/artifactId&gt;
&lt;version&gt;1.2.17&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
&lt;artifactId&gt;commons-io&lt;/artifactId&gt;
&lt;version&gt;1.3.2&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations --&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
&lt;artifactId&gt;jackson-annotations&lt;/artifactId&gt;
&lt;version&gt;2.9.7&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/com.ibm.mfp/mfp-java-token-validator --&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.ibm.mfp&lt;/groupId&gt;
&lt;artifactId&gt;mfp-java-token-validator&lt;/artifactId&gt;
&lt;version&gt;8.0.2017020112&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/com.ibm.mfp/mfp-security-checks-base --&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.ibm.mfp&lt;/groupId&gt;
&lt;artifactId&gt;mfp-security-checks-base&lt;/artifactId&gt;
&lt;version&gt;8.0.2018030404&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.google.code.gson&lt;/groupId&gt;
&lt;artifactId&gt;gson&lt;/artifactId&gt;
&lt;version&gt;2.8.5&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api --&gt;
&lt;dependency&gt;
&lt;groupId&gt;javax.ws.rs&lt;/groupId&gt;
&lt;artifactId&gt;javax.ws.rs-api&lt;/artifactId&gt;
&lt;version&gt;2.0.1&lt;/version&gt;
&lt;/dependency&gt;    

</dependencies>

error message:

nested exception is java.lang.AbstractMethodError: javax/ws/rs/core/Response.readEntity&#40;Ljava/lang/Class&#59;&#41;Ljava/lang/Object&#59;, 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 &amp;&amp; !responseData.isEmpty() ? responseData : &quot;{}&quot;);

and then I was using String responseData = serverResponse.readEntity(String.class); when I changed readEntity to getEntity it worked fine for me.

huangapple
  • 本文由 发表于 2020年8月19日 15:23:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63481908.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定