Jersy 2 Client + JAXB (MessageBodyWriter not found) - java

I'm trying to use Jersy 2 in client mode to post XML to a server but i always get an exception.
I have got only one dependency in my pom file:
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.18</version>
</dependency>
My Java code:
public static void main(String... args) {
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080");
Entity<SimpleClass> entity = Entity.entity(new SimpleClass(), MediaType.APPLICATION_XML_TYPE);
target.request(MediaType.TEXT_XML_TYPE).post(entity);
}
#XmlRootElement(name = "test")
#XmlAccessorType(XmlAccessType.NONE)
public class SimpleClass {
#XmlElement(name = "hello")
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
Exception:
Exception in thread "main" org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/xml, type=class jersey.SimpleClass, genericType=class jersey.SimpleClass.
What I'm doing wrong?

Thank's to peeskillet!
Since Jersey 2.16 you have to add JAX-B support:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-jaxb</artifactId>
<version>2.18</version>
</dependency>
See:
Jersey version issue: MessageBodyReader not found for media type=application/xml

Related

Jersey 2.x post call MessageBodyWriter not found for media type=application/xml

I am using Jersey version 2.29 /java 1.8 on tomcat version 8.5 and trying to retrurn the hasmap<String,String> from jersey rest post service call.
I am getting below exception on server when it is trying to write the hasmap in response.
Aug 23, 2019 10:20:47 PM org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo
SEVERE: MessageBodyWriter not found for media type=application/xml, type=class java.util.LinkedHashMap, genericType=java.util.Map.
Below are the details of pom.xml,server and jersey client side code.
pom.xml
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.1</version> </dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-jaxb</artifactId>
<version>${jersey.version}</version>
</dependency>
Client Code
ClientConfig configuration=new ClientConfig();
Client restClientConfig = ClientBuilder.newClient(configuration);
WebTarget webTarget=restClientConfig.target("http://localhost:8080/messenger/webapi/messages/testMap");
HashMap<String,String> mapStr=new HashMap<String,String>();
mapStr.put("a","1");
mapStr.put("b","2");
webTarget.request()
.accept(MediaType.APPLICATION_XML)
.post(Entity.json(mapStr));
Map<String,String> responseMap = new HashMap<String,String>();
GenericType<Map<String,String>> entity = new GenericType<Map<String,String>>() {};
Response xmlResponse = Response.ok(entity).build();
System.out.println("XMLResponse Is :" + xmlResponse + ":"+ responseMap.size());
Jersey Post Service code
#POST
#Path("/testMap")
#Produces(value = { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
#Consumes(value = { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Map<String,String> postMapMessage(Map<String,String> mapMessage) {
System.out.println("It is been invoked....and this time we will add the new MapMessage");
if(mapMessage!=null)
{
System.out.println("Size of the Map Message:" + mapMessage.size());
mapMessage.put("c","3");
}
return mapMessage;
}
I have tried several solutions found on internet but nothing seems to be working for this.
Can anybody please tell me what wrong I am doing in above code snippet?
I am able to partially fix the issue by creating the below wrapper class.
import java.util.Map;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement
public class JaxrsMapWrapper<T,K> {
private Map<T,K> map;
public JaxrsMapWrapper(){
}
#Override
public String toString() {
return map .toString();
}
public void setMap(Map<T,K> map) {
this.map = map;
}
public Map<T,K> getMap() {
return map;
}
}
By using the above class below getservice returning the typeof Map is working absolutly fine.
#GET
#Path("/mapWarpperReceive")
#Produces({MediaType.APPLICATION_XML})
public JaxrsMapWrapper<String,String> getWarpperMapMsgStr()
{
System.out.println("Returning the MapMessage as String ");
Map<String,String> originalMap=new HashMap<String,String>(){{put("a","a");put("b","b");}};
JaxrsMapWrapper<String,String> jaxRsMapWrapper=new JaxrsMapWrapper<>();
jaxRsMapWrapper.setMap(originalMap);
return jaxRsMapWrapper;
}
But when I am trying to use the same class JaxrsMapWrapper with type of Map it is throwing Error 500 Internal server error while invoking through postman.
#GET
#Path("/customMap")
#Produces({MediaType.APPLICATION_XML})
public JaxrsMapWrapper<String,BookBo> getWarpperMapMsgWithCustomObject()
{
System.out.println("Returning the MapMessage as String and Custom Message ");
Map<String,BookBo> originalMap=new HashMap<>();
originalMap.put("a",new BookBo(1,"Jinesh"));
JaxrsMapWrapper<String,BookBo> jaxRsMapWrapper=new JaxrsMapWrapper();
jaxRsMapWrapper.setMap(originalMap);
return jaxRsMapWrapper;
}
Below is the code for the User defined Java Object BookBo.
#XmlRootElement
public class BookBo implements Serializable{
private Integer id;
private String name;
public BookBo() {
}
public BookBo(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
//getters and setters of the field
}
What am I missing in the above code due to which while writing the Map in response is not working?

Jersey Client Fails To Deserialize to Object

So I am trying to implement a simple Jersey Client that hits a public API to get movie times etc..
https://api.eventcinemas.co.nz/Api/Movies/GetMovies
I have gone through tutorials on how to do this and have implemented two methods that deserialzse the JSON response into:
A String
An Object (POJOs)
The issue is this: the JSON to String method is working correctly, printing the String to console gives me the expected result. However when trying to deserialize to my Java Objects I am always getting null.
I have tried a few simple things such as different dependency versions, different API calls etc but no luck. To save time I have used an online converter to take the JSON response and populate the necessary POJOs for deserialization, I have taken this to be correct.
Would someone be kind enough to point me in the right direction on why I am always getting null, I feel like its something small or silly that I have missed. Thanks in advance!
So starting with my pom.xml dependencies...
pom.xml
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
My Client is as follows:
MoviesClient:
package nz.co.brownbridge.application;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
public class MoviesClient {
protected MoviesResponse getMovieDetails() {
/*JSON to POJO*/
Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target("https://api.eventcinemas.co.nz/Api/Movies/GetMovies");
MoviesResponse response = webTarget.request().accept(MediaType.APPLICATION_JSON_TYPE).get(MoviesResponse.class);
return response;
}
protected String getMovieDetailsString() {
/*JSON to String*/
Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target("https://api.eventcinemas.co.nz/Api/Movies/GetMovies");
String response = webTarget.request().accept(MediaType.APPLICATION_JSON_TYPE).get(String.class);
return response;
}
}
and finally the main() class:
Application Class:
package nz.co.brownbridge.application;
public class Application {
public static void main(String[] args) throws InterruptedException {
MoviesClient moviesClient = new MoviesClient();
String stringResponse = moviesClient.getMovieDetailsString();
MoviesResponse pojoResponse = moviesClient.getMovieDetails();
System.out.println("Printing String Response...");
System.out.println();
System.out.println(stringResponse);
System.out.println();
System.out.println();
System.out.println("Printing POJO Response...");
System.out.println();
System.out.println(pojoResponse);
}
}
Would output the following:
Printing String Response...
//super long but correct string response goes here
Printing POJO Response...
ClassPojo [Data = null, Success = null]

Jersey MessageBodyWriter not found with simple POJO which has empty constructor

This application is just an interface between an mqtt broker and another REST interface, so it is not a full fledged REST application. (Incoming mqtt messages get processed and sent towards the REST interface via POST requests.)
It works fine if I use java.net.HttpURLConnection and Gson to send POST requests with JSON payload, but I wanted to make this work with javax.ws.rs.client.Client and javax.ws.rs.core.Response too, because later on SSL support might be needed, and I already know how to provide that with Jersey. Since I think I have the right dependencies, and the POJO has a very simple structure with empty constructor, I cant find out, what causes the problem. Here are the relevant codes:
The POJO i want to send:
public class NumData {
private String meas_loc_site;
private int meas_loc_id;
private String gmt_event;
private String eu_db_site;
private int eu_db_id;
private int eu_type_code;
private float data_value;
public NumData(){
}
public NumData(String meas_loc_site, int meas_loc_id, String gmt_event, String eu_db_site, int eu_db_id,
int eu_type_code, float data_value) {
super();
this.meas_loc_site = meas_loc_site;
this.meas_loc_id = meas_loc_id;
this.gmt_event = gmt_event;
this.eu_db_site = eu_db_site;
this.eu_db_id = eu_db_id;
this.eu_type_code = eu_type_code;
this.data_value = data_value;
}
public String getMeas_loc_site() {
return meas_loc_site;
}
public void setMeas_loc_site(String meas_loc_site) {
this.meas_loc_site = meas_loc_site;
}
public int getMeas_loc_id() {
return meas_loc_id;
}
public void setMeas_loc_id(int meas_loc_id) {
this.meas_loc_id = meas_loc_id;
}
public String getGmt_event() {
return gmt_event;
}
public void setGmt_event(String gmt_event) {
this.gmt_event = gmt_event;
}
public String getEu_db_site() {
return eu_db_site;
}
public void setEu_db_site(String eu_db_site) {
this.eu_db_site = eu_db_site;
}
public int getEu_db_id() {
return eu_db_id;
}
public void setEu_db_id(int eu_db_id) {
this.eu_db_id = eu_db_id;
}
public int getEu_type_code() {
return eu_type_code;
}
public void setEu_type_code(int eu_type_code) {
this.eu_type_code = eu_type_code;
}
public float getData_value() {
return data_value;
}
public void setData_value(float data_value) {
this.data_value = data_value;
}
}
The utility function which would send the request using jersey libraries:
public static <T> Response sendRequest(String URI, String method, T payload) throws Exception {
Response response = null;
ClientConfig configuration = new ClientConfig();
configuration.property(ClientProperties.CONNECT_TIMEOUT, 30000);
configuration.property(ClientProperties.READ_TIMEOUT, 30000);
Client client = ClientBuilder.newClient(configuration);
WebTarget target = client.target(UriBuilder.fromUri(URI).build());
switch (method) {
case "GET":
response = target.request().header("Content-type", "application/json").get();
break;
case "POST":
System.out.println("test1");
response = target.request().header("Content-type", "application/json").post(Entity.json(payload));
System.out.println("test2");
break;
case "PUT":
response = target.request().header("Content-type", "application/json").put(Entity.json(payload));
break;
case "DELETE":
response = target.request().header("Content-type", "application/json").delete();
break;
default:
throw new Exception("Invalid method type was given to the Utility.sendRequest() method");
}
if (response == null || response.getStatus() == 500 || response.getStatus() == 400 || response.getStatus() == 404) {
throw new Exception("Response is null or the response status code is: 400, 404 or 500");
}
return response;
}
pom.xml dependencies: (version is 2.25.1)
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
I also tried using jersey-media-json-jackson, but the issue remained.
And the stacktrace: (sorry, dont know how to fix the copy here)
org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo
SEVERE: MessageBodyWriter not found for media type=application/json, type=class my.package.NumData, genericType=class my.package.NumData.
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class my.package.NumData, genericType=class my.package.NumData.
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:247)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1130)
at org.glassfish.jersey.client.ClientRequest.doWriteEntity(ClientRequest.java:517)
at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:499)
at org.glassfish.jersey.client.internal.HttpUrlConnector._apply(HttpUrlConnector.java:393)
at org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:285)
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:252)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:684)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:681)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:681)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:437)
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:343)
at eu.mantis.mqtt_mimosa.Utility.sendRequest(Utility.java:53)
at eu.mantis.mqtt_mimosa.Main.sendEnviromentMeasToMimosa(Main.java:155)
at eu.mantis.mqtt_mimosa.Main.messageArrived(Main.java:79)
at org.eclipse.paho.client.mqttv3.internal.CommsCallback.deliverMessage(CommsCallback.java:477)
at org.eclipse.paho.client.mqttv3.internal.CommsCallback.handleMessage(CommsCallback.java:380)
at org.eclipse.paho.client.mqttv3.internal.CommsCallback.run(CommsCallback.java:184)
at java.lang.Thread.run(Unknown Source)

Can't get json from Swagger + Jersey

I have RESTful service based on Jersey 1.18.1 and I want to show my API via Swagger.
Firstly I have to get JSON. I read this instruction: Swagger Core Jersey 1.X Project Setup 1.5. Swagger allows to set up a configuration different methods and I decided to use custom Application subclass. I did everything step by step but I can't get JSON which I have to use for swagger-ui.
What I did:
My custom Application
#ApplicationPath("api/v1")
public class DiscountsApp extends Application{
public DiscountsApp() {
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("1.0.2");
beanConfig.setSchemes(new String[]{"http"});
beanConfig.setHost("localhost:8002");
beanConfig.setBasePath("swaggerapi");
beanConfig.setResourcePackage("alexiuscrow.diploma.endpoints");
beanConfig.setScan(true);
}
#Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new HashSet();
resources.add(ShopsResources.class);
//...
resources.add(com.wordnik.swagger.jaxrs.listing.ApiListingResource.class);
resources.add(com.wordnik.swagger.jaxrs.listing.SwaggerSerializers.class);
return resources;
}
}
ShopsResources
#Path("/shops")
#Api(value="/shops", description="Shops")
public class ShopsResources {
#GET
#Produces(MediaType.APPLICATION_JSON)
#ApiOperation(value = "List shops", httpMethod = "GET",
notes = "List nearest or locality shops",
response = Shops.class, responseContainer = "List")
public String getShops(
#ApiParam( value = "Radius", required = false)
#QueryParam("radius") String radiusParam,
#ApiParam( value = "Latitude", required = true)
#QueryParam("lat") String latParam,
#ApiParam( value = "Longitude", required = true)
#QueryParam("lng") String lngParam) throws SQLException{
//The list of Shops objects is serialized to string
//using the custom GSON serializer and I know
//that there is the better method of the solution of this task.
}
}
}
Some dependencies from pom.xml
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-jersey-jaxrs</artifactId>
<version>1.5.1-M2</version>
</dependency>
After deploy application to Tomcat I tried to get http://localhost:8002/swaggerapi but I've got no result.
I didn't find the swagger.json in root of my application (/tomcat8/webapps/app).
What's wrong?
How can I get JSON with my API?
I did not correctly build the url.
Correct:
http://{host}:{port}/{context root of application}/{path from #ApplicationPath}/swagger.json
In my case: http://localhost:8080/app/api/v1/swagger.json
Thx to Ron.
adding a relative path worked for me (this is using .netcore 1.1)
app.UseSwaggerUI(s => {
s.RoutePrefix = "help";
s.SwaggerEndpoint("../swagger/v1/swagger.json", "MySite");
s.InjectStylesheet("../css/swagger.min.css");
});

REST Jersey Client - unable to parse JSON into POJO class

I am trying to build a rest client using jersey 2.13.
The rest endpoint is in : https://gist.githubusercontent.com/richersoon/ff4dd5c5abe414c5ec4c/raw/4ce49c32e57bf071d052f7efa76f332d60308035/user.json
But when I tried to run the application I got:
Exception in thread "main" org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=text/plain, type=class com.napier.entity.User, genericType=class com.napier.entity.User.
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:173)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:134)
at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:988)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:833)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:768)
at org.glassfish.jersey.client.InboundJaxrsResponse.readEntity(InboundJaxrsResponse.java:96)
at org.glassfish.jersey.client.ScopedJaxrsResponse.access$001(ScopedJaxrsResponse.java:56)
at org.glassfish.jersey.client.ScopedJaxrsResponse$1.call(ScopedJaxrsResponse.java:77)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:397)
at org.glassfish.jersey.client.ScopedJaxrsResponse.readEntity(ScopedJaxrsResponse.java:74)
at com.napier.service.rest.UsersClient.main(UsersClient.java:20)
Here's the code:
public class UsersClient {
public static void main(String[] args) {
Client client = ClientBuilder.newClient();
WebTarget target = client.target(
UriBuilder.fromUri(
"https://gist.githubusercontent.com/richersoon/ff4dd5c5abe414c5ec4c/raw/4ce49c32e57bf071d052f7efa76f332d60308035/user.json"));
Response response = target.request().accept(MediaType.APPLICATION_JSON).get(Response.class);
User user = response.readEntity(User.class);
System.out.println(user);
}
}
Here's the POJO:
#XmlRootElement
public class User {
private String firstname;
private String lastname;
private String photourl;
... setters and getters...
}
Here's the POM:
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.13</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.13</version>
</dependency>
Please guide me because I am totally new to webservices.
Your client is accepting results of media type "application/json", but your REST webservice returns "text/plain". Check this post to see a possible solution: MessageBodyReader not found for media type=application/octet-stream
Seems you are trying to access the wring uri, which is plain text.
I was able to get it to work with this uri, which is the actual .json file
"https://gist.github.com/richersoon/ff4dd5c5abe414c5ec4c#file-user-json"

Categories

Resources