• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Exception in thread "main" org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundExceptio

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I have a simple Spring Boot application to test REST APIs using Jersey Implementation. Here is what I have:

1. Main Spring Boot Application
import org.glassfish.jersey.server.ResourceConfig;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;

import com.mypackage.MyRestController;

@SpringBootApplication
public class MySpringBootApp extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(IDAASpringBootApp.class, args);
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(MySpringBootApp.class);
}

@Bean
 ResourceConfig resourceConfig() {
     return new ResourceConfig().register(MyRestController.class);
 }

}//End of class


2. My Controller

import org.springframework.web.bind.annotation.RestController;
import com.mypackage.Result;
import com.mypackage.ScoreCard;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import java.util.ArrayList;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Controller
@Path("/app")
public class MyRestController {
@GET
@Path("/")
public String Hello() {
return "HELLO REST CONTROLLER";
}

@GET
@Path("/{type}")
public String hello2(@PathParam("type") String type) {
return "hello 2 REST CONTROLLER" + type;
}

@GET
@Path("/Result/{id}")
@Produces({MediaType.APPLICATION_JSON})
public Result getResult(@PathParam("id") String id) {
Result result= new Result();
ScoreCard sc1= new ScoreCard();
ArrayList<ScoreCard> scoreCardList = new ArrayList<ScoreCard>();
sc1.setSubject("Math");
sc1.setGrade("A");

ScoreCard sc2= new ScoreCard();
sc2.setSubject("Science");
sc2.setGrade("B");

scoreCardList.add(sc1);
scoreCardList.add(sc2);
result.setScoreCardList(scoreCardList);

if (id == null || id== "") {
id="01";
}
result.setMessage("Hello REST Java Object " + id);
return result;
}

}//End of class


3. Jersey REST test client

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJaxbJsonProvider;

import com.mypackage.Result;
import com.mypackage.ScoreCard;


public class RestClient {

public static void main(String[] args) {
String path="Result/100";
String url="http://localhost:9080/app/";
ClientConfig config = new ClientConfig();
config.register(JacksonJaxbJsonProvider.class);
Client client = ClientBuilder.newClient(config);

WebTarget webTarget = client.target(url).path(path);

Invocation.Builder invocationBuilder =  webTarget.request(MediaType.APPLICATION_JSON);

Response response = invocationBuilder.get();
Result result=response.readEntity(Result.class);
if (result==null) {
System.out.println("result is null");
}else {
System.out.println(result.getMessage());
for(int i=0;i< result.getScoreCardList().size();i++) {
ScoreCard scoreCard= (ScoreCard) result.getScoreCardList().get(i);
System.out.println("Subject : " + scoreCard.getSubject() + " Grade: " + scoreCard.getGrade() + "\n");
}
}
}
}


4. My maven pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.mypackage</groupId>
 <artifactId>MyREST</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>war</packaging>

 
   <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>2.2.1.RELEASE</version>
   </parent>
   <dependencies>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
       <dependency>
        <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-tomcat</artifactId>
          <scope>provided</scope>
       </dependency>
    <!--  Jersey Server -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-jersey</artifactId>
   </dependency>
<!--  Jersey Client -->
<dependency>
  <groupId>org.glassfish.jersey.core</groupId>
  <artifactId>jersey-client</artifactId>
  </dependency>
  <dependency>
  <groupId>com.fasterxml.jackson.jaxrs</groupId>
  <artifactId>jackson-jaxrs-json-provider</artifactId>
  <version>2.4.0</version>
</dependency>
       
       </dependencies>
       
</project>




But when  I run the RestClient I get the below exception

Exception in thread "main" org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=text/html;charset=ISO-8859-1, type=class com.quest.idaa.odm.domain.Result, genericType=class com.quest.idaa.odm.domain.Result.
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:208)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:132)
at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1071)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:850)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:784)
at org.glassfish.jersey.client.ClientResponse.readEntity(ClientResponse.java:297)
at org.glassfish.jersey.client.InboundJaxrsResponse$1.call(InboundJaxrsResponse.java:91)
at org.glassfish.jersey.internal.Errors.process(Errors.java:292)
at org.glassfish.jersey.internal.Errors.process(Errors.java:274)
at org.glassfish.jersey.internal.Errors.process(Errors.java:205)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:365)
at org.glassfish.jersey.client.InboundJaxrsResponse.runInScopeIfPossible(InboundJaxrsResponse.java:240)
at org.glassfish.jersey.client.InboundJaxrsResponse.readEntity(InboundJaxrsResponse.java:88)
 
Bartender
Posts: 7488
171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Where does com.quest.idaa.odm.domain.Result come into play? Is that the same class you tried to obscure as com.mypackage.Result?
 
Eins india
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lol. So much for obscuring.....yes that is the same class. And that is just a pojo class that holds the results.
I have googled about this exception and tried various recommendations. the last change I have included is the one in the RestClient registerd the ClientConfig with JacksonJaxbJsonprovide as:

 
Bartender
Posts: 2266
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You may also need these dependencies on your pom.xml:
<dependency>
   <groupId>org.glassfish.jersey.media</groupId>
   <artifactId>jersey-media-json-jackson</artifactId>
   <version>2.22</version>
</dependency>
<dependency>
       <groupId>org.glassfish.jersey.media</groupId>
       <artifactId>jersey-media-moxy</artifactId>
       <version>2.24</version>
   </dependency>
Reference:
https://stackoverflow.com/questions/23442440/messagebodyreader-not-found-for-media-type-application-json/25313319
 
Eins india
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apologize. It was my bad. the port I mentioned in the RestClient was 9080. It was supposed to be 8080. Thanks for the responses.
Weird that the exception was totally unrelated..
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic