diff --git a/gemeinsamforschen/pom.xml b/gemeinsamforschen/pom.xml index ba3f26e754d59a8cfb790ee60bf13fd023dafe04..34de8955c326892757f510bcbfbb83dce4db8ab4 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 da79e7c65be8325c939f6ab7e087fee6c3df3a1c..0000000000000000000000000000000000000000 --- 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 33804165f66cd1178e3931e7dd4da0542b77e0c9..e68d85c166f61f8eec469f5e62ed0e1375532f8d 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 427faa40e36dcf8a792d8bc2dcf83ece882c0f1a..fbe928715ff45f4a71336674566d75030f0d483d 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 354a511913f2a83d3c0be39c5e7374b67d5bf479..5cfe6f211a65465901be877ceacee0ac42cf7662 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 0000000000000000000000000000000000000000..205c0005b665424d0e2ed31a26d702db8c079e51 --- /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 0000000000000000000000000000000000000000..6cb5823456039900b25fd25bf837303799f7ec21 --- /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