JQuery is a JavaScript framework for DOM manipulation and navigation. Jersey is the reference implementation of the Java API for RESTful Web Services (JAX-RS).
Furthermore JQuery can be used to realize AJAX based browser clients. On the other hand Jersey is useful to implement server-side RESTful webservices, that handles the JSON format.
The best way to understand something new is to make an easy example. The following example contains a Spring based Jersey REST endpoint, that generates JSON data. This endpoint expects two numbers, which will be summed up. You can enter this two numbers on web page. JQuery code will get the data from the REST endpoint and will append the result to the DOM. By clicking a refresh button the data will be refreshed.
(1) First I created a Maven "webapp" project with the following command.
(4) Next I created a MathService, that calculates the sum of two numbers.
public class MathService {
public int sum(int firstNumber, int secondNumber) {
return firstNumber + secondNumber;
}
}
(5) JAXB based class for the REST response.
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class SumRestResult {
private Integer firstNumber;
private Integer secondNumber;
public Integer getFirstNumber() {
return firstNumber;
}
public void setFirstNumber(Integer firstNumber) {
this.firstNumber = firstNumber;
}
public Integer getSecondNumber() {
return secondNumber;
}
public void setSecondNumber(Integer secondNumber) {
this.secondNumber = secondNumber;
}
private Integer total;
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
}
(6) Jersey annotated business logic class. After a request the sum method will be called.
import com.sun.jersey.spi.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
@Path("/sum")
public class SumRestEndpoint {
@Inject
private MathService mathService;
@GET
@Produces("application/json")
public SumRestResult sum(
@QueryParam("firstNumber") String firstNumber,
@QueryParam("secondNumber") String secondNumber) {
SumRestResult result = new SumRestResult();
result.setFirstNumber(Integer.parseInt(firstNumber));
result.setSecondNumber(Integer.parseInt(secondNumber));
result.setTotal(mathService.sum(Integer.parseInt(firstNumber), Integer.parseInt(secondNumber)));
return result;
}
}
(7) After that I set up the Spring application context file for dependency injection.
(8) Now you have to configure the Jersey Spring servlet.
Jersey Spring Servletcom.sun.jersey.spi.spring.container.servlet.SpringServletcom.sun.jersey.config.property.resourceConfigClasscom.sun.jersey.api.core.PackagesResourceConfigcom.sun.jersey.config.property.packagesorg.developers.blog.ajaxrest.exampleJersey Spring Servlet/services/*
(9) Now you can start your application with the command mvn jetty:run. If you call the address http://localhost:8080/ajax-rest-jason-webapp/services/sum?firstNumber=1&secondNumber=2 in your browser, you will get the following JSON data.
Nice example but you have a ton of errors in this from not instantiating your classes to declaring multiple element Id's with the same name in your HTML
How do you user a complex type for the input of a get request?
Say, you wanted to make your method like this:
public SumRestResult sum(SumRequest sr) { ...}
Is that possible? Not sure how it would interact with the query params or if you posted a data object from the jquery .ajax method post data { firstNumber: 1, secondNumber:2 } .
nice tutorial (ignoring typos) ...
by the way instead of downloading the script (step 2), you could as well just import above mentioned url
<code><script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"> </script></code>