Close

JAX-RS - @Path Examples

JAX-RS JAVA EE 

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

@Path("orders")
public class OrderService {

@GET
@Path("{orderId:\\d{3,5}}")
public String getOrders(@PathParam("orderId") String orderId) {
return "orderId: " + orderId;
}

@GET
@Path("{orderId:[a-z]\\d{2,3}}")
public String getOrders2(@PathParam("orderId") String orderId) {
return "orderId: " + orderId;
}
}
Original Post




import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

@Path("orders")
public class OrderService {

@GET
@Path("{orderId}")
public String getOrders(@PathParam("orderId") String orderId) {
return "orderId: " + orderId;
}

@GET
@Path("summary")
public String getOrdersSummary() {
return "orders summary";
}
}
Original Post




import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("/hi")
public class HelloWorldRestService {

@GET
public String getHelloMessage(){
return "Hi there!";
}
}
Original Post




@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;
}
}
Original Post
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);
}
Original Post




See Also