Hello,
Today i tried to build my first Client REST WEB Serviece (using Jersey), i make very simple example, but some how to get a error every time
public class Location {
private int locationId;
private String ort;
private String PLZ;
private String strasse;
private String country;
//Setters und Getters
}
package inventory;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import beans.Location;
@Path("/location/plz")
public class LocationService {
@GET
@Path("/get")
@Produces("text/html")
public Location getLocationInHTML() {
Location loc = new Location();
loc.setCountry("Deutschland");
loc.setLocationId(1);
loc.setOrt("Berlin");
loc.setPLZ("01019");
loc.setStrasse("Augustusplatz 13");
return loc;
}
}
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
public class LocationClientGet {
// http://localhost:8080/StammDaten/eumonis/location/plz/get
public static void main(String[] args) {
try {
Client client = Client.create();
WebResource webResource = client.resource("http://localhost:8080/StammDaten/eumonis/location/plz/get");
ClientResponse response = webResource.accept("text/html").get(ClientResponse.class);
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}
String output = response.getEntity(String.class);
System.out.println("Output from Server .... \n");
System.out.println(output);
} catch (Exception e) {
e.printStackTrace();
}
}
}
HTML Code:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>inventory;test</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>
<param-value>com.sun.jersey.api.core.PackagesResourceConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/eumonis/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
I added every Jar for this Project, but get error:
HTML Code:
javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class beans.Location,
and Java type class beans.Location, and MIME media type text/html was not found
what the Problem, and how can i solve it?