Jaxb > Jakarta, Java 8 > Java 11+, Tomcat 9 > Tomcat 10, Jersey 2 > Jersey 3, fix java errors when using java 11+ due to the improper configuration of modules in jersey<2.7. This is a huge change to dependencies and the import. Replace javax imports by jakarta imports and fix moxy xml dependencies. Please pay attention to the imports since old javax dependencies will explicitly cause Class Not found or worse : silently fail
This commit is contained in:
79
pom.xml
79
pom.xml
@@ -9,10 +9,11 @@
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<jersey.version>2.28</jersey.version>
|
||||
<jackson.version>2.9.6</jackson.version>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<jersey.version>3.0.4</jersey.version>
|
||||
<org.json.version>20210307</org.json.version>
|
||||
<servlet.api.version>5.0.0</servlet.api.version>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
@@ -31,11 +32,12 @@
|
||||
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>4.0.1</version>
|
||||
<scope>provided</scope>
|
||||
<groupId>jakarta.servlet</groupId>
|
||||
<artifactId>jakarta.servlet-api</artifactId>
|
||||
<version>${servlet.api.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Jersey -->
|
||||
@@ -67,30 +69,51 @@
|
||||
<groupId>org.glassfish.jersey.media</groupId>
|
||||
<artifactId>jersey-media-sse</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey.media</groupId>
|
||||
<artifactId>jersey-media-jaxb</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- JSONObject -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.json</groupId>
|
||||
<artifactId>json</artifactId>
|
||||
<version>${org.json.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- JAXB -->
|
||||
<dependency>
|
||||
<groupId>javax.xml.bind</groupId>
|
||||
<artifactId>jaxb-api</artifactId>
|
||||
<version>2.2.11</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.xml.bind</groupId>
|
||||
<artifactId>jaxb-core</artifactId>
|
||||
<version>2.2.11</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.xml.bind</groupId>
|
||||
<artifactId>jaxb-impl</artifactId>
|
||||
<version>2.2.11</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.activation</groupId>
|
||||
<artifactId>activation</artifactId>
|
||||
<version>1.1.1</version>
|
||||
</dependency>
|
||||
|
||||
<groupId>jakarta.xml.bind</groupId>
|
||||
<artifactId>jakarta.xml.bind-api</artifactId>
|
||||
<version>3.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.xml.bind</groupId>
|
||||
<artifactId>jaxb-impl</artifactId>
|
||||
<version>3.0.1</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.eclipse.persistence</groupId>
|
||||
<artifactId>org.eclipse.persistence.moxy</artifactId>
|
||||
<version>3.0.0</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>3.3.1</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
41
src/main/java/fr/istic/chat/message/Message.java
Normal file
41
src/main/java/fr/istic/chat/message/Message.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package fr.istic.chat.message;
|
||||
|
||||
import jakarta.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
|
||||
public class Message {
|
||||
|
||||
private Long id;
|
||||
private String content;
|
||||
private String date;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public String getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(String date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return id + ":" + date + ">" + content;
|
||||
}
|
||||
|
||||
}
|
||||
86
src/main/java/fr/istic/chat/message/MessageList.java
Normal file
86
src/main/java/fr/istic/chat/message/MessageList.java
Normal file
@@ -0,0 +1,86 @@
|
||||
package fr.istic.chat.message;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class MessageList {
|
||||
|
||||
private static MessageList INSTANCE = new MessageList();
|
||||
|
||||
public static final MessageList getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
private static long CURRENT_ID = 0;
|
||||
|
||||
private synchronized static Long getNextID() {
|
||||
return CURRENT_ID++;
|
||||
}
|
||||
|
||||
public synchronized static Long getHighestID() {
|
||||
return CURRENT_ID;
|
||||
}
|
||||
|
||||
/* L'utilisation d'un bloc static est une très mauvaise idée de façon générale.
|
||||
* Ce code est appelé au chargement de la classe par le Classloader
|
||||
* On l'utilise ici pour remplir les messages :
|
||||
*/
|
||||
static {
|
||||
|
||||
Message m = new Message();
|
||||
m.setContent("ceci est un test");
|
||||
INSTANCE.createMessage(m);
|
||||
|
||||
m = new Message();
|
||||
m.setContent("test 2");
|
||||
INSTANCE.createMessage(m);
|
||||
|
||||
m = new Message();
|
||||
m.setContent("test 3");
|
||||
INSTANCE.createMessage(m);
|
||||
|
||||
}
|
||||
|
||||
private Map<Long, Message> messagesMap = new HashMap<Long, Message>();
|
||||
|
||||
public synchronized Message createMessage(Message message) {
|
||||
message.setId(getNextID());
|
||||
message.setDate(new Date().toString());
|
||||
messagesMap.put(message.getId(), message);
|
||||
return message;
|
||||
}
|
||||
|
||||
public synchronized Message getMessage(Long id) {
|
||||
return messagesMap.get(id);
|
||||
}
|
||||
|
||||
public synchronized void delMessage(Long id) {
|
||||
messagesMap.remove(id);
|
||||
}
|
||||
|
||||
public synchronized List<Message> getMessages() {
|
||||
return new ArrayList<Message>(messagesMap.values());
|
||||
}
|
||||
|
||||
public synchronized List<Message> getMessagesBetween(Long id1, Long id2) {
|
||||
List<Message> messages = new ArrayList<Message>();
|
||||
for (Long i = id1; i <= id2; i++) {
|
||||
messages.add(messagesMap.get(i));
|
||||
}
|
||||
return messages;
|
||||
}
|
||||
|
||||
public synchronized List<Message> getMessagesAfter(Long id1) {
|
||||
List<Message> messages = new ArrayList<Message>();
|
||||
for (Long i = id1; i <= getHighestID(); i++) {
|
||||
if (messagesMap.containsKey(i)) {
|
||||
messages.add(messagesMap.get(i));
|
||||
}
|
||||
}
|
||||
return messages;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
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;
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.PathParam;
|
||||
import jakarta.ws.rs.Produces;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
@Path("/hello")
|
||||
public class Hello {
|
||||
@@ -18,7 +18,7 @@ public class Hello {
|
||||
|
||||
@Path("/{name}")
|
||||
@GET
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
|
||||
public Message getDescription(@PathParam("name") String name) {
|
||||
Message m = new Message();
|
||||
m.name = name;
|
||||
|
||||
26
src/main/java/fr/istic/rest/MessageRessource.java
Normal file
26
src/main/java/fr/istic/rest/MessageRessource.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package fr.istic.rest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import fr.istic.chat.message.Message;
|
||||
import fr.istic.chat.message.MessageList;
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.POST;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.PathParam;
|
||||
import jakarta.ws.rs.Produces;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import jakarta.xml.bind.JAXBElement;
|
||||
|
||||
public class MessageRessource {
|
||||
|
||||
@Path("/after/{id}")
|
||||
@GET
|
||||
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
|
||||
public List<Message> getMessagesAfter(@PathParam("id") Long id){
|
||||
System.out.println("message after" + id);
|
||||
return MessageList.getInstance().getMessagesAfter(id);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,15 +3,15 @@ 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;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.annotation.WebServlet;
|
||||
import jakarta.servlet.http.HttpServlet;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.ws.rs.client.Client;
|
||||
import jakarta.ws.rs.client.ClientBuilder;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
@WebServlet("/client")
|
||||
public class HelloServlet extends HttpServlet {
|
||||
@@ -30,10 +30,6 @@ public class HelloServlet extends HttpServlet {
|
||||
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 {
|
||||
|
||||
42
src/main/java/test/TestApi.java
Normal file
42
src/main/java/test/TestApi.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package test;
|
||||
|
||||
|
||||
import jakarta.ws.rs.client.Client;
|
||||
import jakarta.ws.rs.client.ClientBuilder;
|
||||
import jakarta.ws.rs.client.Entity;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import jakarta.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
public class TestApi {
|
||||
|
||||
//TODO : Adapter l'URL en fonction de votre resource :
|
||||
public static final String URL = "http://localhost:8080/pr.tp.services/api";
|
||||
|
||||
@XmlRootElement(name="message")
|
||||
public static class ChatMessage {
|
||||
public Long id;
|
||||
public String content;
|
||||
public String date;
|
||||
@Override
|
||||
public String toString() {
|
||||
return id + ":" + content+ " at "+ date;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Client client = ClientBuilder.newClient();
|
||||
|
||||
//Post a message :
|
||||
ChatMessage m = new ChatMessage();
|
||||
m.content="test";
|
||||
Response response = client.target(URL).path("messages").request(MediaType.APPLICATION_JSON_TYPE).post(Entity.entity(m, MediaType.APPLICATION_XML));
|
||||
System.out.println("Response "+ response.getStatus());
|
||||
|
||||
//Get all the messages
|
||||
ChatMessage[] messages = client.target(URL).path("messages").request(MediaType.APPLICATION_JSON_TYPE).get(ChatMessage[].class);
|
||||
for (ChatMessage chatMessage : messages) {
|
||||
System.out.println(chatMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,7 @@
|
||||
<?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">
|
||||
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
|
||||
<servlet>
|
||||
<servlet-name>Jersey Web Application</servlet-name>
|
||||
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
|
||||
|
||||
Reference in New Issue
Block a user