Skip to content
Snippets Groups Projects
Commit fdf50d81 authored by Hallvard Trætteberg's avatar Hallvard Trætteberg
Browse files

Oppsett av restapi og restserver

parent 5e7e9e2c
No related branches found
No related tags found
No related merge requests found
...@@ -7,7 +7,7 @@ import todolist.core.TodoList; ...@@ -7,7 +7,7 @@ import todolist.core.TodoList;
import todolist.core.TodoModel; import todolist.core.TodoModel;
@SuppressWarnings("serial") @SuppressWarnings("serial")
class TodoModule extends SimpleModule { public class TodoModule extends SimpleModule {
private static final String NAME = "TodoModule"; private static final String NAME = "TodoModule";
......
[fileMenu]
recentFiles = []
...@@ -140,5 +140,7 @@ ...@@ -140,5 +140,7 @@
<module>core</module> <module>core</module>
<module>fxutil</module> <module>fxutil</module>
<module>fxui</module> <module>fxui</module>
<module>restapi</module>
<module>restserver</module>
</modules> </modules>
</project> </project>
<?xml version="1.0" encoding="UTF-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>it1901.todolist</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>restapi</artifactId>
<dependencies>
<dependency>
<groupId>it1901.todolist</groupId>
<artifactId>core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.11.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<version>2.1.5</version>
</dependency>
<dependency>
<groupId>jakarta.inject</groupId>
<artifactId>jakarta.inject-api</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<!-- Run the checkstyle code quality tool -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin>
<!-- Run the spotbugs code quality tool -->
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
</plugin>
<!-- Configure jacoco code coverage -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package todolist.restapi;
import jakarta.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import todolist.core.TodoList;
import todolist.core.TodoModel;
@Path(TodoModelService.TODO_MODEL_SERVICE_PATH)
public class TodoModelService {
public static final String TODO_MODEL_SERVICE_PATH = "todo";
@Inject
private TodoModel todoModel;
@GET
@Produces(MediaType.APPLICATION_JSON)
public TodoModel getTodoModel() {
return todoModel;
}
private TodoList getTodoList(String name, boolean throwWhenMissing) {
for (TodoList todoList : getTodoModel()) {
if (name.equals(todoList.getName())) {
return todoList;
}
}
if (throwWhenMissing) {
throw new IllegalArgumentException("No TodoList by the name " + name);
}
return null;
}
/**
* Returns the TodoList with the provided name.
*
* @param name the name of the todo list
*/
@GET
@Path("/{name}")
@Produces(MediaType.APPLICATION_JSON)
public TodoList getTodoList(@PathParam("name") String name) {
return getTodoList(name, true);
}
private boolean putTodoList(String name, TodoList todoList) {
TodoList existingTodoList = getTodoList(name, false);
boolean added = false;
if (existingTodoList == null) {
existingTodoList = new TodoList();
existingTodoList.setName(name);
getTodoModel().addTodoList(existingTodoList);
added = true;
}
if (todoList != null) {
existingTodoList.setDeadline(todoList.getDeadline());
}
return added;
}
/**
* Adds a TodoList with the given name, if it doesn't exist already.
*
* @param name the name of the todo list to add
*/
@PUT
@Path("/{name}")
@Produces(MediaType.APPLICATION_JSON)
public boolean putTodoList(@PathParam("name") String name) {
return putTodoList(name, null);
}
/**
* Adds or modifies the TodoList with the given name, and sets the deadline.
*
* @param todoList the todoList to add or modify
*/
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public boolean putTodoList(TodoList todoList) {
return putTodoList(todoList.getName(), todoList);
}
/**
* Renames a TodoList.
*
* @param fromName the oldName
* @param toName the newName
*/
@POST
@Path("/rename")
@Produces(MediaType.APPLICATION_JSON)
public boolean renameTodoList(@QueryParam("from") String fromName, @QueryParam("from") String toName) {
TodoList todoList = getTodoList(fromName, true);
todoList.setName(toName);
return true;
}
/**
* Removes the TodoList by the given name.
*
* @param name the name
*/
@DELETE
@Path("/{name}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public boolean removeTodoList(String name) {
TodoList todoList = getTodoList(name, true);
getTodoModel().removeTodoList(todoList);
return true;
}
}
package todolist.restapi;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import todolist.json.TodoModule;
@Provider
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class TodoModuleObjectMapperProvider implements ContextResolver<ObjectMapper> {
private final ObjectMapper objectMapper;
public TodoModuleObjectMapperProvider() {
objectMapper = new ObjectMapper().registerModule(new TodoModule());
}
@Override
public ObjectMapper getContext(final Class<?> type) {
return objectMapper;
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>it1901.todolist</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>restserver</artifactId>
<packaging>war</packaging>
<properties>
<slf4jVersion>1.7.25</slf4jVersion>
<jerseyVersion>2.28</jerseyVersion>
</properties>
<dependencies>
<dependency>
<groupId>it1901.todolist</groupId>
<artifactId>core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>it1901.todolist</groupId>
<artifactId>restapi</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.11.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4jVersion}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4jVersion}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jerseyVersion}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jerseyVersion}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jerseyVersion}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>${jerseyVersion}</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<!-- Run the checkstyle code quality tool -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin>
<!-- Run the spotbugs code quality tool -->
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
</plugin>
<!-- Configure jacoco code coverage -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.31.v20200723</version>
<configuration>
<httpConnector>
<port>8999</port>
</httpConnector>
<stopKey>quit</stopKey>
<stopPort>9000</stopPort>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
package todolist.restserver;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.server.ResourceConfig;
import todolist.core.TodoList;
import todolist.core.TodoModel;
import todolist.restapi.TodoModuleObjectMapperProvider;
import todolist.restapi.TodoModelService;
public class TodoConfig extends ResourceConfig {
/**
* Initialize this TodoConfig.
*
* @param todoModel todoModel instance to serve
*/
public TodoConfig(TodoModel todoModel) {
register(TodoModelService.class);
register(TodoModuleObjectMapperProvider.class);
register(JacksonFeature.class);
register(new AbstractBinder() {
@Override
protected void configure() {
bind(todoModel);
}
});
}
/**
* Initialize this TodoConfig with a default TodoModel.
*/
public TodoConfig() {
this(createDefaultTodoModel());
}
private static TodoModel createDefaultTodoModel() {
TodoModel todoModel = new TodoModel();
TodoList todoList1 = new TodoList();
todoList1.setName("todo1");
todoModel.addTodoList(todoList1);
TodoList todoList2 = new TodoList();
todoList2.setName("todo2");
todoModel.addTodoList(todoList2);
return todoModel;
}
}
<web-app
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>todo</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>todolist.restserver.TodoConfig</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>todo</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment