Last active
July 24, 2017 22:28
-
-
Save antic183/849a7a4d58e8f7546c1344fc4d5cc837 to your computer and use it in GitHub Desktop.
rest example with wildfly swarm and jersey
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM java:openjdk-8-jdk | |
ADD target/demo-swarm.jar /opt/demo-swarm.jar | |
EXPOSE 8081 | |
ENTRYPOINT ["java", "-jar", "/opt/demo-swarm.jar"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pack and run your application | |
1. cd yourProjectPath | |
2. maven package | |
3. java -jar target/demo-swarm.jar | |
open http://localhost:8080 | |
# the same with docker | |
first: execute points 1 and 2 fom above! | |
> docker build -t repo/image_name . # create image with dockerfile | |
> docker images # see now your created image | |
> docker run --name rest_example -it -p 8081:8080 IMAGE_ID|IMAGE_NAME /bin/bash | |
open http://localhost:8081 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<script src="bower_components/jquery/dist/jquery.min.js"></script> | |
<style> | |
* { | |
line-height: 130%; | |
font-family: Arial, Verdana, sans-serif; | |
} | |
#wrapper { | |
width: 60%; | |
margin: 50px auto; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="wrapper"> | |
<h1>Rest service with wildfly-swarm and jersey</h1> | |
<p> | |
<button id="post1">Get Request to api/v1/service/info</button> | |
</p> | |
<p> | |
<button id="setAuthorizationToken">set authorization token</button> | |
<button id="removeAuthorizationToken" style="display: none;">remove authorization token</button> | |
</p> | |
<p> | |
<b>you need the autorization header befor you add a new user</b><br/> | |
<input type="text" placeholder="username" id="username"/> | |
<button id="post2">Post Request to api/v1/service/addUser</button> | |
</p> | |
</div> | |
<script> | |
(function ($) { | |
'use strict'; | |
$(document).on('click', '#post1', function (e) { | |
$.get('api/v1/service/info', function (res) { | |
alert(res); | |
}); | |
}); | |
$(document).on('click', '#setAuthorizationToken', function (e) { | |
$.ajaxSetup({ | |
headers: { | |
'Authorization': "Bearer abcdddeeefffffff-1234" | |
} | |
}); | |
$('#removeAuthorizationToken').toggle(10); | |
$('#setAuthorizationToken').toggle(10); | |
}); | |
$(document).on('click', '#removeAuthorizationToken', function (e) { | |
$.ajaxSetup({ | |
headers: { | |
'Authorization': null | |
} | |
}); | |
$('#removeAuthorizationToken').toggle(10); | |
$('#setAuthorizationToken').toggle(10); | |
}); | |
$(document).on('click', '#post2', function (e) { | |
var username = $('#username').val(); | |
if (username.trim() !== "") { | |
$.post('api/v1/service/addUser', {user: $('#username').val()}, function (res, status, xhr) { | |
// console.info("user id = " + xhr.getResponseHeader('user-id')); | |
console.info(xhr.getAllResponseHeaders()); | |
$('#username').val(''); | |
}); | |
} else { | |
alert('Username?'); | |
} | |
}); | |
})(jQuery); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.example</groupId> | |
<artifactId>rest_example</artifactId> | |
<name>WildFly Swarm Rest Example</name> | |
<version>1.0.0</version> | |
<packaging>war</packaging> | |
<properties> | |
<version.wildfly.swarm>2017.7.0</version.wildfly.swarm> | |
<maven.compiler.source>1.8</maven.compiler.source> | |
<maven.compiler.target>1.8</maven.compiler.target> | |
<failOnMissingWebXml>false</failOnMissingWebXml> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
</properties> | |
<dependencyManagement> | |
<dependencies> | |
<dependency> | |
<groupId>org.wildfly.swarm</groupId> | |
<artifactId>bom-all</artifactId> | |
<version>${version.wildfly.swarm}</version> | |
<scope>import</scope> | |
<type>pom</type> | |
</dependency> | |
</dependencies> | |
</dependencyManagement> | |
<dependencies> | |
<!-- WildFly Swarm Fractions --> | |
<!-- ... --> | |
<!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-server --> | |
<dependency> | |
<groupId>com.sun.jersey</groupId> | |
<artifactId>jersey-server</artifactId> | |
<version>1.9</version> | |
</dependency> | |
<!-- https://mvnrepository.com/artifact/com.sun.jersey.contribs/jersey-multipart --> | |
<dependency> | |
<groupId>com.sun.jersey.contribs</groupId> | |
<artifactId>jersey-multipart</artifactId> | |
<version>1.9</version> | |
</dependency> | |
</dependencies> | |
<build> | |
<finalName>demo</finalName> | |
<plugins> | |
<plugin> | |
<groupId>org.wildfly.swarm</groupId> | |
<artifactId>wildfly-swarm-plugin</artifactId> | |
<version>${version.wildfly.swarm}</version> | |
<!-- favor IPv4Stack as IP6 can cause trobule on some operating systems and WildFly-Swarm --> | |
<!--ONLY FOR DOCKER--> | |
<configuration> | |
<properties> | |
<java.net.preferIPv4Stack>true</java.net.preferIPv4Stack> | |
</properties> | |
</configuration> | |
<!--ONLY FOR DOCKER--> | |
<executions> | |
<execution> | |
<goals> | |
<goal>package</goal> | |
</goals> | |
</execution> | |
</executions> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.rest_example; | |
import javax.ws.rs.ApplicationPath; | |
import javax.ws.rs.core.Application; | |
@ApplicationPath("/api/v1") | |
public class RestConfig extends Application { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.rest_example; | |
import javax.ws.rs.*; | |
import javax.ws.rs.core.Response; | |
@Path("/service") | |
public class Service { | |
@GET | |
@Path("info/") | |
public Response getInfo() { | |
String output = "Service works!"; | |
return Response.status(200).entity(output) | |
.header("Access-Control-Allow-Origin", "*") | |
.header("Access-Control-Allow-Methods", "GET") | |
.build(); | |
} | |
@POST | |
@Path("addUser/") | |
public Response addUser( | |
@DefaultValue("") @FormParam("user") String username | |
, @DefaultValue("") @HeaderParam("Authorization") String jwtToken | |
) { | |
if (!jwtToken.equals("Bearer abcdddeeefffffff-1234") || username.trim().isEmpty()) { | |
return Response.status(401) | |
.header("Access-Control-Allow-Origin", "*") | |
.header("Access-Control-Allow-Methods", "POST") | |
.build(); | |
} | |
User user = new User(username); | |
// persist user | |
return Response.status(201).entity("") | |
.header("user-id", user.getId()) | |
.header("Access-Control-Allow-Origin", "*") | |
// 'access-control-expose-headers' allow the client to read the response-header 'user-id' | |
.header("Access-Control-Expose-Headers", "user-id") | |
.header("Access-Control-Allow-Methods", "POST") | |
.build(); | |
} | |
@OPTIONS | |
@Path("addUser/") | |
public Response getAccountDataOptions() { | |
// CORS: Browser first make an options request when you set own header from client side | |
return Response.status(201).entity("") | |
.header("Access-Control-Allow-Origin", "*") | |
.header("Access-Control-Allow-Headers", "authorization") | |
.header("Access-Control-Allow-Methods", "OPTIONS") | |
.build(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.rest_example; | |
import java.util.UUID; | |
public class User { | |
private final String username; | |
private final String id; | |
public User(String username) { | |
this.username = username; | |
this.id = UUID.randomUUID().toString().replaceAll("-", ""); | |
} | |
public String getUsername() { | |
return username; | |
} | |
public String getId() { | |
return id; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment