JAX-RS JAVA EE
import com.logicbig.example.Customer;import com.logicbig.example.CustomerDataService;import javax.ws.rs.*;import javax.ws.rs.core.MediaType;import javax.ws.rs.core.Response;import java.util.List;@Path("customers")public class CustomerRestService { private CustomerDataService dataService = CustomerDataService.getInstance(); @GET @Produces(MediaType.APPLICATION_JSON) public List<Customer> getCustomers() { return dataService.getCustomerList(); } @POST @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces(MediaType.TEXT_PLAIN) public String createCustomer(@FormParam("name") String name, @FormParam("address") String address, @FormParam("phone-number") String phoneNumber) { return dataService.addCustomer(name, address, phoneNumber); } @GET @Path("{id}") @Produces(MediaType.APPLICATION_JSON) public Response getCustomer(@PathParam("id") String id) { Customer customer = dataService.getCustomerById(id); if (customer == null) { return Response.status(Response.Status.NOT_FOUND) .build(); } else { return Response.ok() .entity(customer) .build(); } }}
import com.logicbig.example.Customer;import com.logicbig.example.CustomerDataService;import javax.ws.rs.*;import javax.ws.rs.core.MediaType;import java.util.List;@Path("customers")public class CustomerRestService { private CustomerDataService dataService = CustomerDataService.getInstance(); @GET @Produces(MediaType.APPLICATION_JSON) public List<Customer> getCustomers() { return dataService.getCustomerList(); } @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.TEXT_PLAIN) public String createCustomer(Customer newCustomer) { return dataService.addCustomer(newCustomer); } @GET @Path("{id}") @Produces(MediaType.APPLICATION_JSON) public Customer getCustomer(@PathParam("id") String id) { return dataService.getCustomerById(id); }}
package com.logicbig.example;import javax.ws.rs.POST;import javax.ws.rs.Path;@Path("/")public class ResourceExample { @Path("/test1") @POST public String handle(String entityParam) { System.out.println(entityParam); return "entity parameter: " + entityParam; } @Path("/test2") @POST public String handle2(MyMsg entityParam) { System.out.println(entityParam); return "entity parameter: " + entityParam; }}
@Path("/")public class MyResource { @Path("/test") @GET public Response handle() { Response r = Response.ok("test with GET, body content") .build(); return r; } @Path("/test2") @GET public Response handle2() { Response r = Response.ok("test2 with GET, body content") .build(); return r; } @Path("/test2") @POST public Response handle3() { Response r = Response.ok("test2 with POST, body content") .build(); return r; } @Path("/test2") @HEAD public Response handle4() { Response r = Response.ok("test2 with HEAD, body content") .build(); return r; } @Path("/test3") @GET public Response handle5() { Response r = Response.ok("test3 with GET, body content") .build(); return r; } @Path("/test3") @OPTIONS public Response handle6() { Response r = Response.ok("test3 with OPTIONS, body content") .header("Allow", "GET") .header("Allow", "OPTIONS") .build(); return r; } @Path("/test4") @DELETE public Response handle7() { Response r = Response.ok("test4 with DELETE, body content") .build(); return r; } @Path("/test4") @PUT public Response handle8() { Response r = Response.ok("test4 with PUT, body content") .build(); return r; }}
public interface AppResource<T> { @PUT @Path("{newId}") @Consumes(MediaType.APPLICATION_XML) String create(T t, @PathParam("newId") long newId); @DELETE @Path("{id}") String delete(@PathParam("id") long id); @POST @Consumes(MediaType.APPLICATION_XML) String update(T t); @GET @Produces(MediaType.APPLICATION_XML) List<T> list(); @GET @Path("{id}") @Produces(MediaType.APPLICATION_XML) T byId(@PathParam("id") long id);}