Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The registered message body readers compatible with the MIME media type are: application/json;charset=UTF-8

I'm using Spring Rest API in server side and jersey API in client side.

I'm creating a screen where it will fetch last 5 customer redeem transaction.

From server side i'm returning list of RedeemTransactionDetails and accepting the same in client side.

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.

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

<dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.19.3</version>
    </dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-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
like image 374
light_ray Avatar asked Feb 23 '17 08:02

light_ray


2 Answers

This problem is known issue and various answer is already available in stackoverflow.

Now I am suggesting you to follow some suggestions to solve your issue.

Suggestion#1:

You can add genson jar file by using the following dependency in your pom.xml file

<dependency>
    <groupId>com.owlike</groupId>
    <artifactId>genson</artifactId>
    <version>1.4</version>
</dependency>

Documentation can be found at: https://owlike.github.io/genson/

Then clean your project and build and then run.

Resource Link: https://stackoverflow.com/a/25754441/2293534

Suggestion#2:

You can add jersy bundle jar file in your pom.xml file.

<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

Suggestion#3:

Check your entitiy contains @XmlRootElement annotations or not. If not, then please add it.

Resource Link: https://stackoverflow.com/a/7388605/2293534

Suggestion#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

Resource Link: https://stackoverflow.com/a/4955831/2293534

like image 156
SkyWalker Avatar answered Oct 23 '22 22:10

SkyWalker


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.

<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.

like image 24
VinPro Avatar answered Oct 23 '22 21:10

VinPro