1

Restfull client is not working

My Restfull Service on server works well. I call the service with a form and i get a beautifull response in JSON Format. But if i write a client with a main method for the Service the Client will not work.

Exception on following client code

List<Materialgrundlagen> matList = jsonAnswer.readEntity(new   GenericType<List<Materialgrundlagen>>() {});

Exception

MessageBodyReader not found for media type=application/json, type=interface java.util.List, genericType=java.util.List.

I use following components

  1. Glassfish 4.1.1
  2. The newest OSGI Bundle from EclipseLink
  3. Jersy whis is included in Glassfish under modules

This is the form which is working on Browser

<!Doctype html>
<html>
<head>
<title>Read VAN</title>
<meta charset="UTF-8"></meta>


</head>
<body>
 <form action="http://hilweb05:8080/RestServiceExample5/matgi/SomeService/" method="POST">
<label for="van">Systembezeichnung:</label>
<input name="van" />
<br/>
<input type="submit" value="Submit" />
</form>
</body>
</html>

This is the data class (JPA Entity and XxmlRootElement)

package de.ml;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@XmlRootElement
@NamedQuery(name="SomeDataClass.findSome", query="SELECT m FROM SomeDateClass m where m.van like :van")
public class SomeDataClass implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = -4567908639347398145L;
long f1;
long f2;
String f3;
double f4;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public long getF1() {
    return f1;
}
public void setId(long f1) {
    this.f1 = f1;
}
public long getf2() {
    return f2;
}
public void setF2(long f2) {
    this.f2 = f2;
}
public String getF3() {
    return f3;
}
public void setF3(String f3) {
    this.van = f4;
}
public double getF4() {
    return f4;
}
public void setF4(double f4) {
    this.f4 = f4;
}
}

This is the service class

package de.ml;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.TypedQuery;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
@Stateless
@LocalBean
@Path("/SomeService")
public class SomeService {
/*
 * 
 * http://localhost:8080/RestServiceExample5/matgi/SomeService/
 */
 @PersistenceContext(unitName="somePU",type=PersistenceContextType.TRANSACTION)
EntityManager em;
/**
 * Default constructor.
 */
public MatGi2() {

} 


/**
 * Retrieves representation of an instance of MatGi
 * 
 * @return an instance of String
*/
@POST
@Produces({ MediaType.APPLICATION_JSON })
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public List<SomeDataClass> matgiGET(@FormParam("f1") String f1) {
    System.out.println("Beginn GET Method.");
    List<Materialgrundlagen> list = null;
    TypedQuery<Materialgrundlagen> query = em.createNamedQuery("Materialgrundlagen.findSome", Materialgrundlagen.class);
query.setParameter("van", "%"  + van + "%");
list=   query.getResultList();
System.out.println("End GET Method");


return list;
}

 /**
 * PUT method for updating or creating an instance of MatGi
 * 
 * @content content representation for the resource
 * @return an HTTP response with content of the updated or created resource.
*/
@PUT
@Consumes({ MediaType.APPLICATION_JSON })
public void resourceMethodPUT(SomeDataClass  content) {
    // TODO Auto-generated method stub
    throw new UnsupportedOperationException();
}
}

And know the client that not work

package de.ml.restclient;
import java.util.List;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.client.ClientConfig;

public class RestServiceExample5ClientJSON {
    public static void main(String[] args) {
    ClientConfig config = new ClientConfig();
    Client client = ClientBuilder.newClient(config);
    WebTarget target = client
            .target("http://hilweb05:8080/RestServiceExample5/matgi/SomeService/");
    Form form = new Form().param("f1", "value1");

    Response jsonAnswer = target.request()
            .accept(MediaType.APPLICATION_JSON).post(Entity.form(form));
    if (jsonAnswer.getStatus() != 200) {
        throw new RuntimeException("Hier laeuft was falsch : "
                + jsonAnswer.getStatus());
    }
    // Hier steigt er aus ...
    List<SomeDataClass> matList = jsonAnswer.readEntity(new  GenericType<List<SomeDataClass>>() {});
    for (SomeDataClass m : matList) {
        System.out.println(m.getF1() + " " + m.getF2() + " "
        + m.getF3());
    }       
}

}
michaeldbjava
  • 114
  • 10
  • Hava a look at [this](http://stackoverflow.com/questions/23442440/messagebodyreader-not-found-for-media-type-application-json) post! – Arthur Eirich Jul 11 '16 at 13:45
  • Hello Arthur, thanks a lot for your comment.I have a stupid question. Where can i find "jersy jackson media" as jar file? I can not work with maven. My Work Computer can not access to internet. – michaeldbjava Jul 11 '16 at 13:54
  • Ahhhm I am afraid you can't get the jar without the internet access unless someone brings it to you on a USB device. You can find it e. g. [here](http://www.java2s.com/Code/JarDownload/jersey-media/jersey-media-json-2.0-m04-1-sources.jar.zip) – Arthur Eirich Jul 11 '16 at 13:57
  • Thanks a lot Arthur ...:-) – michaeldbjava Jul 11 '16 at 13:59
  • Good Mornung Arthur, i try to make a jar from the source in a separete eclipse project. Following class is missing: AbstractMessageReaderWriterProvider. Have you some idea? – michaeldbjava Jul 12 '16 at 06:12
  • Some addition information. I add following jars to my eclipse classpath: jersy-client.jar, jersy-common.jar, jersy-container-servlet-core.jar, jersy-media-jaxb.jar, jersy-server.jar – michaeldbjava Jul 12 '16 at 06:18
  • I would highly recommend you to get the internet access and use maven as you don't want to load all the needed dependencies manually. – Arthur Eirich Jul 12 '16 at 06:29
  • Yes, you are right.... Thanks Arthur ... – michaeldbjava Jul 12 '16 at 06:37
  • Hi arthur, i change to resteay (only for the client:-)) If i write something like this System.out.println(jsonAnswer.readEntity(String.class)); it will be executed. But if want to map it with my business object get an exception at following line: List matList = jsonAnswer.readEntity(new GenericType>() {}); – michaeldbjava Jul 12 '16 at 07:02
  • The content of exception is: Unrecognized Type: [null] – michaeldbjava Jul 12 '16 at 07:03

0 Answers0