Jersey is not introduced here. It is a restful Web Service implemented by Sun. For details, refer to the following link: use jersey and Apache Tomcat to build a restful web service.
What I want to talk about here is how to obtain the JSON data of the front-end Ajax request. I did not talk about this part after I searched it on the Internet, or I used the client to simulate it. I have successfully tested it here. I will first give a method.
To put it bluntly, first go to the Code:
The first is the JSP page Ajax request:
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
Background resource processing:
@Path("/")public class HelloWorld { @GET @Path("/helloWorld") @Produces(MediaType.TEXT_PLAIN) public String abc() { String ret = "Hello World!"; return ret; } @POST @Path("/update") @Produces("application/json") public Person update(@FormParam(value = "a1") String a1, @FormParam(value = "a2") String a2) { System.out.println(a1); System.out.println(a2); return new Person("testPerson", "22"); } @GET @Path("/requestTest") @Produces("application/json") public Person update(@Context HttpServletRequest request) { System.out.println("request:" + request.getParameter("a11")); System.out.println("request:" + request.getParameter("a22")); System.out.println("request:" + request.getQueryString()); return new Person("testPerson", "33"); }}
Note that the first Ajax request is the update method of the post request, @ formparam (value = "A1") string A1, @ formparam (value = "A2 ") string A2 here A1 and A2 are parameters sent from the front end, which can be used in the update method. However, if there is a lot of JSON data, we cannot list them one by one in the method. Another method is to obtain the @ context httpservletrequest request through the request. Note that the request method is get. In this way, we can easily get the parameters sent from the front-end JSON. You can also obtain the value in the session.
This is the test. Thank you.