public class Student { private int id; private String name; private int age; // Getter and setter methods // Constructor } import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @Path("/students") public class StudentResource { @GET @Produces(MediaType.APPLICATION_JSON) public Response getAllStudents() { // Logic to retrieve all students // Return the list of students as JSON } @GET @Path("/{id}") @Produces(MediaType.APPLICATION_JSON) public Response getStudentById(@PathParam("id") int id) { // Logic to retrieve a student by id // Return the student as JSON } @POST @Consumes(MediaType.APPLICATION_JSON) public Response createStudent(Student student) { // Logic to create a new student // Return the created student as JSON } @PUT @Path("/{id}") @Consumes(MediaType.APPLICATION_JSON) public Response updateStudent(@PathParam("id") int id, Student student) { // Logic to update a student // Return the updated student as JSON } @DELETE @Path("/{id}") public Response deleteStudent(@PathParam("id") int id) { // Logic to delete a student // Return the response status } } import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; @ApplicationPath("/api") public class StudentApplication extends Application { // No additional logic is required } <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>Student Management API</display-name> <!-- Configure the JAX-RS servlet --> <servlet> <servlet-name>Jersey RESTful Application</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>com.example.StudentApplication</param-value> <!-- Specify the fully qualified name of your StudentApplication class --> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Map the JAX-RS servlet to all requests starting with /api --> <servlet-mapping> <servlet-name>Jersey RESTful Application</servlet-name> <url-pattern>/api/*</url-pattern> </servlet-mapping> </web-app>


上一篇:
下一篇:
切换中文