Upgrade to java 11, Basic JAX-RS setup

This commit is contained in:
simon987 2019-03-18 19:22:06 -04:00
parent 1bbc342b7f
commit 4aab07f4cd
7 changed files with 194 additions and 8 deletions

61
pom.xml
View File

@ -8,29 +8,36 @@
<artifactId>music-graph</artifactId>
<version>0.1</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<version>3.6.2</version>
<version>3.8.0</version>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<mainClass>net.simon987.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
@ -51,5 +58,45 @@
<version>3.1.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.containers/jersey-container-grizzly2-http -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
<version>2.28</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.inject/jersey-hk2 -->
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.28</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-json-jackson -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.28</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.4.0-b180830.0359</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-osgi -->
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-osgi</artifactId>
<version>2.4.0-b180830.0438</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-core -->
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0.1</version>
</dependency>
</dependencies>
</project>

View File

@ -1,9 +1,40 @@
package net.simon987;
import net.simon987.logging.LogManager;
import net.simon987.webapi.Index;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.server.ResourceConfig;
import java.net.URI;
import java.util.logging.Logger;
public class Main {
private final static Logger LOGGER = LogManager.getLogger();
public static void main(String[] args) {
System.out.println("Hello, world");
LOGGER.info("test");
startHttpServer();
}
private static void startHttpServer() {
ResourceConfig rc = new ResourceConfig();
rc.registerClasses(Index.class);
rc.registerClasses(JacksonFeature.class);
try {
HttpServer server = GrizzlyHttpServerFactory.createHttpServer(
new URI("http://localhost:8080/"),
rc);
server.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,21 @@
package net.simon987.logging;
import java.util.logging.*;
public class LogManager {
private static Logger logger;
static {
logger = Logger.getLogger("music-graph");
Handler handler = new StdOutHandler();
logger.setUseParentHandlers(false);
logger.addHandler(handler);
}
public static Logger getLogger() {
return logger;
}
}

View File

@ -0,0 +1,24 @@
package net.simon987.logging;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;
public class MusicGraphFormatter extends Formatter {
private static final DateFormat dateFormat = new SimpleDateFormat("MM-dd HH:mm:ss");
@Override
public String format(LogRecord record) {
Date date = new Date();
return String.format(
"[%s] (MG) %s: %s\n",
dateFormat.format(date), record.getLevel(), record.getMessage()
);
}
}

View File

@ -0,0 +1,20 @@
package net.simon987.logging;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.StreamHandler;
public class StdOutHandler extends StreamHandler {
StdOutHandler() {
super(System.out, new MusicGraphFormatter());
this.setLevel(Level.ALL);
}
@Override
public synchronized void publish(LogRecord record) {
super.publish(record);
flush();
}
}

View File

@ -0,0 +1,20 @@
package net.simon987.models;
public class ApiInfo {
private String name;
private String version;
public ApiInfo(String name, String version) {
this.name = name;
this.version = version;
}
public String getName() {
return name;
}
public String getVersion() {
return version;
}
}

View File

@ -0,0 +1,23 @@
package net.simon987.webapi;
import net.simon987.models.ApiInfo;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/")
public class Index {
private static final ApiInfo INFO = new ApiInfo(
"music-graph",
"0.1"
);
@GET
@Produces(MediaType.APPLICATION_JSON)
public ApiInfo get() {
return INFO;
}
}