Initial commit

This commit is contained in:
ynn
2019-03-17 16:47:56 +01:00
commit 8669e8ad51
8 changed files with 527 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
package fr.istic.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.XmlRootElement;
@Path("/hello")
public class Hello {
@XmlRootElement
public static class Message {
public String name;
public String greetings;
}
@Path("/{name}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Message getDescription(@PathParam("name") String name) {
Message m = new Message();
m.name = name;
m.greetings = "Hello";
return m;
}
}

View File

@@ -0,0 +1,51 @@
package fr.istic.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.XmlRootElement;
@WebServlet("/client")
public class HelloServlet extends HttpServlet {
// TODO : Adapter l'URL en fonction de votre resource (si vous utilisez un
// runner par exemple)
public static final String URL = "http://localhost:8080/pr.tp.services/api";
@XmlRootElement(name = "message")
public static class SomeMessage {
public String name;
public String greetings;
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// Ceci n'est utile que pour JAXB 2.3.1 et disparaitra plus tard :
System.setProperty("com.sun.xml.bind.v2.bytecode.ClassTailor.noOptimize",
"true");
PrintWriter out = resp.getWriter();
resp.setContentType("text/plain");
try {
Client client = ClientBuilder.newClient();
String name = "Toto";
SomeMessage serverResponse = client.target(URL).path("hello").path(name)
.request(MediaType.APPLICATION_JSON_TYPE).get(SomeMessage.class);
out.println(serverResponse.greetings + " " + serverResponse.name);
} catch (Exception e) {
out.println("No server response");
}
}
}

View File

@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="3.1"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>fr.istic.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>