调用readEntity转换自定义类时抛出MessageBodyProviderNotFoundException异常
Hey there, let's fix this MessageBodyProviderNotFoundException issue you're facing. The root cause here is that your Jersey client doesn't have the necessary JSON processing provider to convert the incoming JSON response into your MyTable class. Even though your service is sending valid JSON and your entity class has the required getters/setters/empty constructor, the client side is missing the components to handle deserialization.
Step 1: Add the Jersey JSON Media Provider Dependency
You need to include the Jersey JSON processing module in your project to enable JSON-to-object deserialization.
If you're using Maven, add this to your pom.xml:
<dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> <version><!-- Use the same version as your Jersey core dependencies --></version> </dependency>
If you're using Gradle, add this to your build.gradle:
implementation 'org.glassfish.jersey.media:jersey-media-json-jackson:{your-jersey-version}'
Make sure the version matches the rest of your Jersey dependencies (like jersey-client, jersey-server) to avoid compatibility conflicts.
Step 2: Register the JSON Provider in Your Client
Once the dependency is added, you need to tell your Jersey client to use the Jackson JSON provider. Update your MyClient.java code to register the feature:
Client client = ClientBuilder.newClient( new ClientConfig() .register(LoggingFeature.class) .register(JacksonFeature.class) // Add this line to enable JSON processing );
JacksonFeature is the simplified, recommended way to enable Jackson-based JSON handling. If you need more customization later, you can register JacksonJaxbJsonProvider.class directly instead.
Step 3: Clean Up Your Service Code (Optional but Recommended)
Your MyService.java uses GenericEntity unnecessarily here. Since MyTable is a concrete class (not a generic collection like List<MyTable>), you can simplify the response code to:
@PermitAll @GET @Produces(MediaType.APPLICATION_JSON) public Response test() { MyTable table = new MyTable(); table.setId(1); table.setName("mesa de teste"); return Response.ok(table).build(); // No need for GenericEntity }
This works exactly the same and makes your code cleaner.
Step 4: Verify Entity Class (If Issues Persist)
Your MyTable class already has the required empty constructor and proper getters/setters, which should be enough for Jackson to deserialize it. If you still run into issues, you can add explicit Jackson annotations to map fields (though this shouldn't be necessary for standard naming):
import com.fasterxml.jackson.annotation.JsonProperty; private Integer id; private String name; public MyTable() { } public MyTable(@JsonProperty("id") Integer id, @JsonProperty("name") String name) { this.id = id; this.name = name; } // Keep your existing getters and setters
This helps Jackson explicitly recognize the JSON keys and map them to your class fields, especially if there's any naming mismatch.
After making these changes, your client should successfully deserialize the JSON response into a MyTable instance without that error.
内容的提问来源于stack exchange,提问作者Igor Silveira

