Last active
March 31, 2017 09:46
-
-
Save antic183/46211d9461ea2d0e61126cdfac19ea09 to your computer and use it in GitHub Desktop.
Java EE RestFulService Simple example
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
$(document).ready(function() { | |
$(document).on('submit', '#myform', function(ev) { | |
ev.preventDefault(); | |
var data = $("form#myform").serializeJSON(); | |
console.info(data); | |
$.post("http://localhost:8080/RestFulServer/api/v1/simple/form", data, function(res) { | |
console.info(res); | |
}); | |
}); | |
}); |
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
// ******* JAVA PROJECT "1": ******* | |
import javax.ws.rs.ApplicationPath; | |
import javax.ws.rs.core.Application; | |
@ApplicationPath("/api/v1") | |
public class ApplicationConfiguration 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>WebService Example</title> | |
<script type="text/javascript" src="js/jquery-2.1.1.min.js"></script> | |
<script type="text/javascript" src="js/jquery.serializejson.min.js"></script> | |
<script type="text/javascript" src="js/app.js"></script> | |
</head> | |
<body> | |
<form id="myform" action="http://localhost:8080/RestFulServer/api/v1/simple/form" method="post"> | |
<p><input type="text" name="firstname" placeholder="Firstname" /></p> | |
<p><input type="text" name="lastname" placeholder="Lastname" /></p> | |
<p><input type="Submit" value="Send data" /></p> | |
</form> | |
</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
// ******* CREATE OTHER JAVA PROJECT "2": ******* | |
public class PersonDTO { | |
private String firstname; | |
private String lastname; | |
public String getFirstname() { | |
return firstname; | |
} | |
public void setFirstname(String firstname) { | |
this.firstname = firstname; | |
} | |
public String getLastname() { | |
return lastname; | |
} | |
public void setLastname(String lastname) { | |
this.lastname = lastname; | |
} | |
} |
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
// ******* JAVA PROJECT "1": ******* | |
import javax.xml.bind.annotation.XmlRootElement; | |
@XmlRootElement | |
public class PersonModel { | |
private String firstname; | |
private String lastname; | |
public PersonModel() { | |
super(); | |
} | |
public PersonModel(String firstName, String lastName) { | |
super(); | |
this.firstname = firstName.toUpperCase(); | |
this.lastname = lastName.toUpperCase(); | |
} | |
public String getFirstname() { | |
return firstname; | |
} | |
public void setFirstname(String firstName) { | |
this.firstname = firstName; | |
} | |
public String getLastname() { | |
return lastname; | |
} | |
public void setLastname(String lastName) { | |
this.lastname = lastName; | |
} | |
} |
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
// ******* CREATE OTHER JAVA PROJECT "2": ******* | |
import java.util.List; | |
import javax.ws.rs.client.ClientBuilder; | |
import javax.ws.rs.client.Entity; | |
import javax.ws.rs.core.GenericType; | |
import javax.ws.rs.core.MediaType; | |
public class RestFullClient { | |
private PersonDTO request = new PersonDTO(); | |
public RestFullClient() { | |
request.setFirstname("John"); | |
request.setLastname("Doe"); | |
System.out.println("RestClient was started:\n"); | |
System.out.println("Single Post:\n"); | |
getSingleMessage(); | |
System.out.println("--------------------------------------"); | |
System.out.println("\n\nMultiple Post:\n"); | |
getMultipleMessage(); | |
System.out.println("--------------------------------------"); | |
} | |
public static void main(String[] args) { | |
new RestFullClient(); | |
} | |
private void getSingleMessage() { | |
PersonDTO response = ClientBuilder.newClient() | |
.target("http://localhost:8080/RestFulServer/api/v1/simple/getOne").request(MediaType.APPLICATION_JSON) | |
.post(Entity.json(request), PersonDTO.class); | |
System.out.println(response.getFirstname() + ", " + response.getLastname()); | |
} | |
private void getMultipleMessage() { | |
List<PersonDTO> responses = ClientBuilder.newClient() | |
.target("http://localhost:8080/RestFulServer/api/v1/simple/getMultiple/").path("{count}") | |
.resolveTemplate("count", 20).request(MediaType.APPLICATION_JSON) | |
.post(Entity.json(request), new GenericType<List<PersonDTO>>() { | |
}); | |
for (PersonDTO person : responses) { | |
System.out.println(person.getFirstname() + ", " + person.getLastname()); | |
} | |
} | |
} |
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
// ******* JAVA PROJECT "1": ******* | |
import java.util.ArrayList; | |
import java.util.List; | |
// import javax.ejb.Stateless; // --> required by Enterprice Application Project | |
import javax.ws.rs.Consumes; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.POST; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.PathParam; | |
import javax.ws.rs.Produces; | |
import javax.ws.rs.core.MediaType; | |
import rest.models.PersonModel; | |
@Path("simple") | |
// @Stateless // --> required by Enterprice Application Project | |
public class SimpleRestfullService { | |
@GET | |
@Path("info") | |
@Produces({ MediaType.APPLICATION_JSON }) | |
public PersonModel sayHello() { | |
// open "http://localhost:8080/RestFulServer/api/v1/simple/info" | |
return new PersonModel("your Firstname", "your Lastname"); | |
} | |
@POST | |
@Path("form") | |
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) | |
@Produces(MediaType.APPLICATION_JSON) | |
public String sayHello(String input) { | |
// action url "http://localhost:8080/RestFulServer/api/v1/simple/form" | |
return input; | |
} | |
@POST | |
@Path("getOne") // post-request to -> | |
// "http://localhost:8080/RestFulServer/api/v1/simple/getOne" | |
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) | |
public PersonModel getPerson(PersonModel input) { | |
return new PersonModel(input.getFirstname(), input.getLastname()); | |
} | |
@POST | |
@Path("getMultiple/{count}") // post-request to -> | |
// "http://localhost:8080/RestFulServer/api/v1/simple/getMultiple/COUNT" | |
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) | |
public List<PersonModel> getPersonList(PersonModel input, @PathParam("count") int repeatCount) { | |
List<PersonModel> result = new ArrayList<>(); | |
for (int i = 0; i < repeatCount; i++) { | |
result.add(new PersonModel(i + ": " + input.getFirstname(), i + ": " + input.getLastname())); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment