java 已注册的与 MIME 媒体类型兼容的消息正文阅读器是:application/json;charset=UTF-8
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42410658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The registered message body readers compatible with the MIME media type are: application/json;charset=UTF-8
提问by light_ray
I'm using Spring Rest API in server side and jersey API in client side.
我在服务器端使用 Spring Rest API,在客户端使用 jersey API。
I'm creating a screen where it will fetch last 5 customer redeem transaction.
我正在创建一个屏幕,它将获取最后 5 个客户的兑换交易。
From server side i'm returning list of RedeemTransactionDetails and accepting the same in client side.
从服务器端我返回 RedeemTransactionDetails 列表并在客户端接受相同的列表。
I had debugged server side code it's returns the valid list, and in client side response code is 200 , whereas while getting entity i'm getting error from client side.
我调试了服务器端代码,它返回有效列表,在客户端响应代码是 200 ,而在获取实体时,我从客户端收到错误。
Server side:
服务器端:
@RestController
@RequestMapping("/rest/api")
public class CustomerRestController {
@Autowired private CustomerService customerService;
@RequestMapping(value="/redeemTransactionList/{clientId}/{mobileNumber}/{numOfTransaction}" , method=RequestMethod.POST , produces = "application/json; charset=UTF-8")
public @ResponseBody List<RedeemTransactionDetails> redeemTransaction(@PathVariable(value = "clientId") int clientId, @PathVariable(value = "mobileNumber") String mobileNumber , @PathVariable(value="numOfTransaction") int numOfTransaction) {
LOG.debug("We are in redeemTransaction method for user {} " , clientId);
List<RedeemTransactionDetails> redeemList = null ;
try {
redeemList = customerService.redeemTransactionList(clientId, mobileNumber,numOfTransaction);
} catch (Exception e) {
LOG.debug("Excption while fetching redeemTransaction ");
}
return redeemList;
}
}
Client Side :
客户端 :
public List<RedeemTransactionDetails> getRedeemTransactions(String mobileNumber, String clientId, String numberOfTransaction) {
log.debug("inside authenticate() ");
List<RedeemTransactionDetails> result = null;
try{
webResource = client.resource(uri + "/redeemTransactionList").path(clientId).path(mobileNumber).path(numberOfTransaction) ;
ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).post(ClientResponse.class);
if (response.getStatus() != 200) {
log.debug("response.getStatus() : " + response.getStatus() );
throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}
response.getType() ;
result = (List<RedeemTransactionDetails>) response.getEntity(RedeemTransactionDetails.class);
log.debug("user Details " + result);
}
catch(Exception e){
log.debug(e);
}
return result ;
}
}
NOTE: I had used the following dependencies in pom xml file
注意:我在 pom xml 文件中使用了以下依赖项
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.19.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-databind</artifactId>
<version>2.8.6</version>
</dependency>
EDIT:
编辑:
ERROR LOG
错误日志
SEVERE: A message body reader for Java class com.prom.via.rest.dto.RedeemTransactionDetails, and Java type class com.prom.via.rest.dto.RedeemTransactionDetails, and MIME media type application/json;charset=UTF-8 was not found
Feb 23, 2017 4:52:17 PM com.sun.jersey.api.client.ClientResponse getEntity
SEVERE: The registered message body readers compatible with the MIME media type are:
*/* ->
com.sun.jersey.core.impl.provider.entity.FormProvider
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.ReaderProvider
com.sun.jersey.core.impl.provider.entity.DocumentProvider
com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General
com.sun.jersey.core.impl.provider.entity.EntityHolderReader
采纳答案by VinPro
The error indicates JerseyClient may not have been configured properly to scan Provider packages. Check your web.xml if 'jersey.config.server.provider.packages' property is configured to include 'com.prom.via.rest.dto' package that contains your JAXB classes.
该错误表明 JerseyClient 可能未正确配置以扫描 Provider 包。如果 'jersey.config.server.provider.packages' 属性配置为包含包含 JAXB 类的 'com.prom.via.rest.dto' 包,请检查您的 web.xml。
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Register resources and providers under com.prom.via.rest.dto package. -->
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.prom.via.rest.dto</param-value>
</init-param>
</servlet>
Also, as SkyWalker indicated check if RedeemTransactionDetails is annotated with @XmlRootElement annotation or not.
此外,正如 SkyWalker 指出的那样,检查 RedeemTransactionDetails 是否使用 @XmlRootElement 批注进行批注。
回答by SkyWalker
This problem is known issue and various answer is already available in stackoverflow.
这个问题是已知问题,stackoverflow 中已经提供了各种答案。
Now I am suggesting you to follow some suggestions to solve your issue.
现在我建议您按照一些建议来解决您的问题。
Suggestion#1:
建议#1:
You can add genson jar file by using the following dependency in your pom.xml file
您可以通过在 pom.xml 文件中使用以下依赖项来添加 genson jar 文件
<dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>1.4</version>
</dependency>
Documentation can be found at: https://owlike.github.io/genson/
可以在以下位置找到文档:https: //owlike.github.io/genson/
Then clean your project and build and then run.
然后清理您的项目并构建然后运行。
Resource Link: https://stackoverflow.com/a/25754441/2293534
资源链接:https: //stackoverflow.com/a/25754441/2293534
Suggestion#2:
建议#2:
You can add jersy bundle jar file in your pom.xml file.
您可以在 pom.xml 文件中添加 jersy bundle jar 文件。
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.19.3</version>
</dependency>
This may also can solve the issue sometimes.
这有时也可以解决问题。
Resource Link: https://stackoverflow.com/a/23192776/2293534
资源链接:https: //stackoverflow.com/a/23192776/2293534
Suggestion#3:
建议#3:
Check your entitiy contains @XmlRootElement
annotations or not. If not, then please add it.
检查您的实体是否包含@XmlRootElement
注释。如果没有,那么请添加它。
Resource Link: https://stackoverflow.com/a/7388605/2293534
资源链接:https: //stackoverflow.com/a/7388605/2293534
Suggestion#4:
建议#4:
jonbros suggested that instead of using the assembly plugin for maven use the shade plugin!
You can read Read problem and full solution from here: http://jersey.576304.n2.nabble.com/issue-with-POST-when-packaging-into-jar-td5460103.html
jonbros 建议不要使用 maven 的程序集插件,而是使用 shade 插件!
您可以从这里阅读阅读问题和完整解决方案:http: //jersey.576304.n2.nabble.com/issue-with-POST-when-packaging-into-jar-td5460103.html
Resource Link: https://stackoverflow.com/a/4955831/2293534