Skip to content
Snippets Groups Projects
Commit f2973259 authored by Julian Dehne's avatar Julian Dehne
Browse files

Finished Munschkin Example

parent 18407941
No related branches found
No related tags found
No related merge requests found
...@@ -38,6 +38,12 @@ ...@@ -38,6 +38,12 @@
<!-- Hier kommen ganz viele Jersey Sachen --> <!-- Hier kommen ganz viele Jersey Sachen -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.glassfish.jersey.containers</groupId> <groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId> <artifactId>jersey-container-servlet</artifactId>
...@@ -55,10 +61,9 @@ ...@@ -55,10 +61,9 @@
<dependency> <dependency>
<groupId>org.glassfish.jersey.media</groupId> <groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId> <artifactId>jersey-media-multipart</artifactId>
<version>2.26</version> <version>${jersey.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId> <artifactId>jackson-core</artifactId>
...@@ -77,6 +82,29 @@ ...@@ -77,6 +82,29 @@
<version>2.4.5</version> <version>2.4.5</version>
</dependency> </dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-xjc</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>7.0.40</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>
\ No newline at end of file
package unipotsdam.gf.config;
import org.apache.log4j.Logger;
import org.glassfish.jersey.server.ResourceConfig;
import javax.ws.rs.ApplicationPath;
@ApplicationPath("/")
public class MyRESTAPIApp extends ResourceConfig {
private Logger logger = org.apache.log4j.LogManager
.getLogger(MyRESTAPIApp.class);
public MyRESTAPIApp() {
packages("unipotsdam.gf.service");
register(org.glassfish.jersey.filter.LoggingFilter.class);
property(
"jersey.config.beanValidation.enableOutputValidationErrorEntity.server",
"true");
logger.info("Initiated Logger");
logger.info("Initiated Server");
}
}
\ No newline at end of file
package unipotsdam.gf.model; package unipotsdam.gf.model;
import javax.xml.bind.annotation.XmlRootElement;
/** /**
* Created by dehne on 24.04.2018. * Created by dehne on 24.04.2018.
*/ */
@XmlRootElement(name = "Munschkin")
public class Munschkin { public class Munschkin {
private int munschkinId; private int munschkinId;
private String lastName; private String lastName;
...@@ -11,6 +14,11 @@ public class Munschkin { ...@@ -11,6 +14,11 @@ public class Munschkin {
private String BadThings; private String BadThings;
private int strength; private int strength;
public Munschkin() {
}
/*
public Munschkin(int munschkinId, String lastName, String firstName, String badThings, int strength) { public Munschkin(int munschkinId, String lastName, String firstName, String badThings, int strength) {
this.munschkinId = munschkinId; this.munschkinId = munschkinId;
this.lastName = lastName; this.lastName = lastName;
...@@ -18,10 +26,9 @@ public class Munschkin { ...@@ -18,10 +26,9 @@ public class Munschkin {
BadThings = badThings; BadThings = badThings;
this.strength = strength; this.strength = strength;
} }
*/
public Munschkin() {
}
public int getMunschkinId() { public int getMunschkinId() {
return munschkinId; return munschkinId;
......
...@@ -12,14 +12,14 @@ import javax.ws.rs.PathParam; ...@@ -12,14 +12,14 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
@Path("/hello") @Path("/api")
public class RestyMunschkin { public class RestyMunschkin {
// This method is called if HTML is request // This method is called if HTML is request
@GET @GET
@Produces(MediaType.TEXT_HTML) @Produces(MediaType.TEXT_HTML)
@Path("/{id}") @Path("/hello/{id}")
public String sayHtmlHello(@PathParam("id") String id) { public String sayHtmlHello(@PathParam("id") String id) {
MunschkinLoader munschkinLoader = new MunschkinLoader(); MunschkinLoader munschkinLoader = new MunschkinLoader();
Munschkin munschkin = munschkinLoader.loadMunschkin(Integer.parseInt(id)); Munschkin munschkin = munschkinLoader.loadMunschkin(Integer.parseInt(id));
...@@ -27,5 +27,15 @@ public class RestyMunschkin { ...@@ -27,5 +27,15 @@ public class RestyMunschkin {
.toString() + "</body></h1>" + "</html> "; .toString() + "</body></h1>" + "</html> ";
} }
// This method is called if HTML is request
@GET
@Produces(MediaType.APPLICATION_XML)
@Path("/munschkin/{id}")
public Munschkin getMunschkin(@PathParam("id") String id) {
MunschkinLoader munschkinLoader = new MunschkinLoader();
Munschkin munschkin = munschkinLoader.loadMunschkin(Integer.parseInt(id));
return munschkin;
}
} }
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
<init-param> <init-param>
<param-name>jersey.config.server.provider.packages</param-name> <param-name>jersey.config.server.provider.packages</param-name>
<param-value> <param-value>
unipotsdam.gf unipotsdam.gf, com.fasterxml.jackson.jaxrs.json
</param-value> </param-value>
</init-param> </init-param>
...@@ -33,6 +33,11 @@ ...@@ -33,6 +33,11 @@
<load-on-startup>1</load-on-startup> <load-on-startup>1</load-on-startup>
</servlet> </servlet>
<servlet-mapping>
<servlet-name>rest-server</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<filter> <filter>
<filter-name>CorsFilter</filter-name> <filter-name>CorsFilter</filter-name>
......
/**
* Created by dehne on 25.04.2018.
*/
function xmlToString(xmlData) {
var xmlString;
//IE
if (window.ActiveXObject) {
xmlString = xmlData.xml;
}
// code for Mozilla, Firefox, Opera, etc.
else {
xmlString = (new XMLSerializer()).serializeToString(xmlData);
}
return xmlString;
}
$(document).ready(function () {
$.ajax({
type: "get",
url: "http://localhost:8080/gemeinsamforschen/rest/api/munschkin/1",
dataType: "xml",
success: function (data) {
/* handle data here */
//var munschkin = $.parseXML(data)
$("#munschkin").append(xmlToString(data));
},
error: function (xhr, status) {
alert("Server down .... ogottogottogottt")
}
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JSexample</title>
<script src="http://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script src="./js/munschkinloader.js" type="application/javascript"></script>
</head>
<body>
Hi Munschkinlieberhaber
<div id="munschkin"></div>
</body>
</html>
\ 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