From f29732599f5040ef01a26e6107ec143991f81f6d Mon Sep 17 00:00:00 2001
From: Julian Dehne <julian.dehne@uni-potsdam.de>
Date: Wed, 25 Apr 2018 10:42:49 +0200
Subject: [PATCH] Finished Munschkin Example

---
 gemeinsamforschen/pom.xml                     | 32 +++++++++++++++--
 .../unipotsdam/gf/config/MyRESTAPIApp.java    | 24 -------------
 .../java/unipotsdam/gf/model/Munschkin.java   | 11 ++++--
 .../unipotsdam/gf/service/RestyMunschkin.java | 14 ++++++--
 .../src/main/webapp/WEB-INF/web.xml           |  7 +++-
 .../src/main/webapp/js/munschkinloader.js     | 36 +++++++++++++++++++
 .../src/main/webapp/jsexample.html            | 17 +++++++++
 7 files changed, 110 insertions(+), 31 deletions(-)
 delete mode 100644 gemeinsamforschen/src/main/java/unipotsdam/gf/config/MyRESTAPIApp.java
 create mode 100644 gemeinsamforschen/src/main/webapp/js/munschkinloader.js
 create mode 100644 gemeinsamforschen/src/main/webapp/jsexample.html

diff --git a/gemeinsamforschen/pom.xml b/gemeinsamforschen/pom.xml
index ba3f26e7..34de8955 100644
--- a/gemeinsamforschen/pom.xml
+++ b/gemeinsamforschen/pom.xml
@@ -38,6 +38,12 @@
 
         <!-- 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>
             <groupId>org.glassfish.jersey.containers</groupId>
             <artifactId>jersey-container-servlet</artifactId>
@@ -55,10 +61,9 @@
         <dependency>
             <groupId>org.glassfish.jersey.media</groupId>
             <artifactId>jersey-media-multipart</artifactId>
-            <version>2.26</version>
+            <version>${jersey.version}</version>
         </dependency>
 
-
         <dependency>
             <groupId>com.fasterxml.jackson.core</groupId>
             <artifactId>jackson-core</artifactId>
@@ -77,6 +82,29 @@
             <version>2.4.5</version>
         </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>
 
 </project>
\ No newline at end of file
diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/config/MyRESTAPIApp.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/config/MyRESTAPIApp.java
deleted file mode 100644
index da79e7c6..00000000
--- a/gemeinsamforschen/src/main/java/unipotsdam/gf/config/MyRESTAPIApp.java
+++ /dev/null
@@ -1,24 +0,0 @@
-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
diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/model/Munschkin.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/model/Munschkin.java
index 33804165..e68d85c1 100644
--- a/gemeinsamforschen/src/main/java/unipotsdam/gf/model/Munschkin.java
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/model/Munschkin.java
@@ -1,9 +1,12 @@
 package unipotsdam.gf.model;
 
+import javax.xml.bind.annotation.XmlRootElement;
+
 /**
  * Created by dehne on 24.04.2018.
  */
 
+@XmlRootElement(name = "Munschkin")
 public class Munschkin {
     private int munschkinId;
     private String lastName;
@@ -11,6 +14,11 @@ public class Munschkin {
     private String BadThings;
     private int strength;
 
+    public Munschkin() {
+
+    }
+
+/*
     public Munschkin(int munschkinId, String lastName, String firstName, String badThings, int strength) {
         this.munschkinId = munschkinId;
         this.lastName = lastName;
@@ -18,10 +26,9 @@ public class Munschkin {
         BadThings = badThings;
         this.strength = strength;
     }
+*/
 
-    public Munschkin() {
 
-    }
 
     public int getMunschkinId() {
         return munschkinId;
diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/service/RestyMunschkin.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/service/RestyMunschkin.java
index 427faa40..fbe92871 100644
--- a/gemeinsamforschen/src/main/java/unipotsdam/gf/service/RestyMunschkin.java
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/service/RestyMunschkin.java
@@ -12,14 +12,14 @@ import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
 import javax.ws.rs.core.MediaType;
 
-@Path("/hello")
+@Path("/api")
 public class RestyMunschkin {
 
 
     // This method is called if HTML is request
     @GET
     @Produces(MediaType.TEXT_HTML)
-    @Path("/{id}")
+    @Path("/hello/{id}")
     public String sayHtmlHello(@PathParam("id") String id) {
         MunschkinLoader munschkinLoader = new MunschkinLoader();
         Munschkin munschkin = munschkinLoader.loadMunschkin(Integer.parseInt(id));
@@ -27,5 +27,15 @@ public class RestyMunschkin {
                 .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;
+    }
+
 
 }
diff --git a/gemeinsamforschen/src/main/webapp/WEB-INF/web.xml b/gemeinsamforschen/src/main/webapp/WEB-INF/web.xml
index 354a5119..5cfe6f21 100644
--- a/gemeinsamforschen/src/main/webapp/WEB-INF/web.xml
+++ b/gemeinsamforschen/src/main/webapp/WEB-INF/web.xml
@@ -18,7 +18,7 @@
         <init-param>
             <param-name>jersey.config.server.provider.packages</param-name>
             <param-value>
-                unipotsdam.gf
+                unipotsdam.gf, com.fasterxml.jackson.jaxrs.json
             </param-value>
         </init-param>
 
@@ -33,6 +33,11 @@
         <load-on-startup>1</load-on-startup>
     </servlet>
 
+    <servlet-mapping>
+        <servlet-name>rest-server</servlet-name>
+        <url-pattern>/rest/*</url-pattern>
+    </servlet-mapping>
+
 
     <filter>
         <filter-name>CorsFilter</filter-name>
diff --git a/gemeinsamforschen/src/main/webapp/js/munschkinloader.js b/gemeinsamforschen/src/main/webapp/js/munschkinloader.js
new file mode 100644
index 00000000..205c0005
--- /dev/null
+++ b/gemeinsamforschen/src/main/webapp/js/munschkinloader.js
@@ -0,0 +1,36 @@
+/**
+ * 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")
+        }
+    });
+
+});
+
diff --git a/gemeinsamforschen/src/main/webapp/jsexample.html b/gemeinsamforschen/src/main/webapp/jsexample.html
new file mode 100644
index 00000000..6cb58234
--- /dev/null
+++ b/gemeinsamforschen/src/main/webapp/jsexample.html
@@ -0,0 +1,17 @@
+<!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
-- 
GitLab