Skip to content
Snippets Groups Projects
Commit 122de451 authored by Gritsch Philipp - Studierendenaccount's avatar Gritsch Philipp - Studierendenaccount
Browse files

Merge branch 'philipp-add-server' into 'master'

Philipp add server

See merge request !2
parents f74ff69d 9d7ccb56
No related branches found
No related tags found
1 merge request!2Philipp add server
......@@ -2,10 +2,41 @@
Make sure java 17 is installed.
Make sure that Maven 3.8.3 is installed on your computer with `mvn --version`. If it is not, follow the instructions here: https://maven.apache.org/install.html.
Make sure you have IntelliJ Ultimate (licence from the university is free) the IDE of choice for Java installed
Test that things are up and running and can work on your machine. Run `mvn package` in the kg-shape-project directory, and you should see a build success message.
Then from the same directory, in the command line run:
## Run "normal" programs
`java -cp target/kg-shape-project-1.0-SNAPSHOT.jar com.kgshape.app.App`
Use your IDE to handle all the dependencies and just hit the play button.
You should see a Hello World message.
![running_main.png](readme/running_main.png)
This may be useful to test small implementations. Later on the test suite will be extended to have also a server environment for testing.
## Run "server" programs
Sooner or later the functionality needs to be provided to our frontend.
A running wildfly (Jakarta EE server) will take care of that.
Checkout [DemoEndpoint.java](src/main/java/com/kgshape/app/rest/demo/DemoEndpoint.java)
as an example for a collection of rest endpoints.
Checkout [ApplicationWideCache.java](src/main/java/com/kgshape/app/service/demo/ApplicationWideCache.java)
as an example for CDI scoped beans (meaning the server takes care of business class creation)
Add the following run configurations to IntelliJ
Maven:
![run_configuration.png](readme/run_configuration.png)
For Debug:
![debug_configuration.png](readme/debug_configuration.png)
Execute the maven configuration, wait until it says deployed, like so:
![server_log_deployed.png](readme/server_log_deployed.png)
Then start the debug configuration. Now you can also debug your server application.
\ No newline at end of file
......@@ -7,6 +7,7 @@
<groupId>com.kgshape.app</groupId>
<artifactId>kg-shape-project</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>kg-shape-project</name>
<!-- FIXME change it to the project's website -->
......@@ -19,12 +20,21 @@
</properties>
<dependencies>
<!-- java application server dependencies, just apis -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-api</artifactId>
<version>9.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<version>3.0.0</version>
<scope>provided</scope>
</dependency>
<!-- project dependencies -->
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>apache-jena-libs</artifactId>
......@@ -41,6 +51,15 @@
<artifactId>log4j-core</artifactId>
<version>2.14.1</version>
</dependency>
<!-- test deps -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
......@@ -96,6 +115,39 @@
<target>17</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
</plugin>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-jar-maven-plugin</artifactId>
<version>6.1.0.Final</version>
<configuration>
<jvmArguments>
<arg>-agentlib:jdwp=transport=dt_socket,address=5005,server=y,suspend=n</arg>
</jvmArguments>
<feature-pack-location>wildfly-preview@maven(org.jboss.universe:community-universe)</feature-pack-location>
<layers>
<layer>jaxrs-server</layer>
<layer>cdi</layer>
<layer>ee-security</layer>
<layer>bean-validation</layer>
<layer>management</layer>
<layer>jaxrs</layer>
</layers>
</configuration>
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
readme/debug_configuration.png

60.7 KiB

readme/run_configuration.png

55.8 KiB

readme/running_main.png

24 KiB

readme/server_log_deployed.png

46.3 KiB

package com.kgshape.app.model.demo;
import jakarta.xml.bind.annotation.XmlRootElement;
import java.util.concurrent.atomic.AtomicLong;
@XmlRootElement
public class ComplexObject {
private long id;
private String name;
public ComplexObject() {
}
public ComplexObject(final long id, final String name) {
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "ComplexObject{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
package com.kgshape.app.rest;
import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application;
/**
* Declares the Rest application
*
* Needed so that the application is initialized correctly on the server
*/
@ApplicationPath("/")
public class RestApplication extends Application {
}
package com.kgshape.app.rest.demo;
import com.kgshape.app.model.demo.ComplexObject;
import com.kgshape.app.service.demo.ApplicationWideCache;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
import jakarta.validation.constraints.NotNull;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import java.util.logging.Logger;
@RequestScoped
@Path("/demo")
public class DemoEndpoint {
// get or create objects if they are needed, with inject
// ApplicationWideCache is ApplicationScoped, meaning it is created on systemstart, and stopped on system stop
@Inject
private ApplicationWideCache applicationWideCache;
// url = localhost:8080/demo
// in general don't comment the url, use intellijs endpoints feature
@GET
public String hello() {
return "hello";
}
// url = localhost:8080/demo/name
// use PUT for update operations
// use POST for Insert operations
// use DELETE for delete operations
@Path("/name")
@PUT
public Response setName(@FormParam("name") final String name) {
this.applicationWideCache.setValue(name);
return Response.ok().build();
}
@Path("/name")
@GET
public String getName() {
return this.applicationWideCache.getValue();
}
@Path("/complexName")
@GET
@Produces(MediaType.APPLICATION_JSON)
public ComplexObject getComplexName() {
return new ComplexObject(1, "complex name");
}
@Path("/complexName")
@PUT
@Consumes(MediaType.APPLICATION_JSON)
public Response logComplexName(@NotNull final ComplexObject complexObject) {
// todo use more sophisticated logger
Logger.getLogger("DemoEndpoint").info(complexObject.toString());
return Response.accepted().build();
}
}
package com.kgshape.app.service.demo;
import jakarta.enterprise.context.ApplicationScoped;
@ApplicationScoped
public class ApplicationWideCache {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
<jboss-web>
<context-root>/</context-root>
</jboss-web>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<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">
</web-app>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment