The document describes a Java class that implements a RESTful web service. It contains annotations that define the resource path, HTTP methods, request parameters, and response types. The class contains multiple methods that retrieve request parameters in different ways, such as from the URI query string, HTTP headers, request body, or the entity itself.
9. @Path("/rest")
@Produces(MediaType.TEXT_PLAIN)
public class DummyResource {
@GET
@Path("/echo1")
public Response queryparam(@QueryParam("value") String param) {...}
@GET
@Path("/echo2")
public Response headerparam(@HeaderParam("X-Echo") String param) {...}
@POST
@Path("/echo3")
public Response formparam(@FormParam("value") String param) {...}
@POST
@Path("/echo4")
public Response entityparam(String param) {...}
}
10. @Path("/rest")
@Produces(MediaType.TEXT_PLAIN)
public class DummyResource {
@GET
@Path("/echo1")
public Response queryparam(@QueryParam("value") String param) {...}
@GET
@Path("/echo2")
public Response headerparam(@HeaderParam("X-Echo") String param) {...}
@POST
@Path("/echo3")
public Response formparam(@FormParam("value") String param) {...}
@POST
@Path("/echo4")
public Response entityparam(String param) {...}
}
Relative URI path for resource
11. @Path("/rest")
@Produces(MediaType.TEXT_PLAIN)
public class DummyResource {
@GET
@Path("/echo1")
public Response queryparam(@QueryParam("value") String param) {...}
@GET
@Path("/echo2")
public Response headerparam(@HeaderParam("X-Echo") String param) {...}
@POST
@Path("/echo3")
public Response formparam(@FormParam("value") String param) {...}
@POST
@Path("/echo4")
public Response entityparam(String param) {...}
}
MIME media type
12. @Path("/rest")
@Produces(MediaType.TEXT_PLAIN)
public class DummyResource {
@GET
@Path("/echo1")
public Response queryparam(@QueryParam("value") String param) {...}
@GET
@Path("/echo2")
public Response headerparam(@HeaderParam("X-Echo") String param) {...}
@POST
@Path("/echo3")
public Response formparam(@FormParam("value") String param) {...}
@POST
@Path("/echo4")
public Response entityparam(String param) {...}
}
Resource methods
13. @Path("/rest")
@Produces(MediaType.TEXT_PLAIN)
public class DummyResource {
@GET
@Path("/echo1")
public Response queryparam(@QueryParam("value") String param) {...}
@GET
@Path("/echo2")
public Response headerparam(@HeaderParam("X-Echo") String param) {...}
@POST
@Path("/echo3")
public Response formparam(@FormParam("value") String param) {...}
@POST
@Path("/echo4")
public Response entityparam(String param) {...}
}
HTTP method annotations: GET, POST, PUT, DELETE, etc.
14. @Path("/rest")
@Produces(MediaType.TEXT_PLAIN)
public class DummyResource {
@GET
@Path("/echo1")
public Response queryparam(@QueryParam("value") String param) {...}
@GET
@Path("/echo2")
public Response headerparam(@HeaderParam("X-Echo") String param) {...}
@POST
@Path("/echo3")
public Response formparam(@FormParam("value") String param) {...}
@POST
@Path("/echo4")
public Response entityparam(String param) {...}
}
Relative URI path for methods
15. @Path("/rest")
@Produces(MediaType.TEXT_PLAIN)
public class DummyResource {
@GET
@Path("/echo1")
public Response queryparam(@QueryParam("value") String param) {...}
@GET
@Path("/echo2")
public Response headerparam(@HeaderParam("X-Echo") String param) {...}
@POST
@Path("/echo3")
public Response formparam(@FormParam("value") String param) {...}
@POST
@Path("/echo4")
public Response entityparam(String param) {...}
}
Is extracted from URI query parameter value
Is extracted from X-Echo header
Is extracted from body parameter value
Entity parameter (w/o annotation)
16.
17.
18.
19.
20. @Provider
@PreMatching
public class DummyFilter implements ContainerRequestFilter {
@Override public void filter(ContainerRequestContext requestContext)
throws IOException {
String echo = requestContext.getHeaderString("X-Echo");
if (echo != null && echo.indexOf("Troopers") != -1) {
requestContext.getHeaders()
.putSingle("X-Echo", "Hello Troopers 2017");
}
}
}
21. @Provider
@PreMatching
public class DummyFilter implements ContainerRequestFilter {
@Override public void filter(ContainerRequestContext requestContext)
throws IOException {
String echo = requestContext.getHeaderString("X-Echo");
if (echo != null && echo.indexOf("Troopers") != -1) {
requestContext.getHeaders()
.putSingle("X-Echo", "Hello Troopers 2017");
}
}
}
Annotated for auto discovery
22. @Provider
@PreMatching
public class DummyFilter implements ContainerRequestFilter {
@Override public void filter(ContainerRequestContext requestContext)
throws IOException {
String echo = requestContext.getHeaderString("X-Echo");
if (echo != null && echo.indexOf("Troopers") != -1) {
requestContext.getHeaders()
.putSingle("X-Echo", "Hello Troopers 2017");
}
}
}
Determines execution order
23.
24. @Provider
public class DummyInterceptor implements ReaderInterceptor {
@Override public Object aroundReadFrom(ReaderInterceptorContext context)
throws Exception {
InputStream old = context.getInputStream();
String text = null;
try (Scanner scanner = new Scanner(old,StandardCharsets.UTF_8.name())) {
text = scanner.useDelimiter("A").next();
}
Pattern p = Pattern.compile(BASE64_REGEXP);
if (p.matcher(text).matches()) {
byte[] bytes = Base64.getDecoder().decode(text);
context.setInputStream(new ByteArrayInputStream(bytes));
return context.proceed();
}
context.setInputStream(new ByteArrayInputStream(text.getBytes()));
return context.proceed();
}
}
81. public class Profile {
private String DisplayName;
private String Email;
private String uid;
public Profile() {}
public Profile(String uid) {
this.uid = uid;
}
public String delete() {
// SOME LOGIC TO FIND PROFILE BY UID AND DELETE IT
return "Deleted";
}
}
82. <script>
var request = new XMLHttpRequest();
var data = '12345';
request.open('POST',
'http://localhost:8080/unsafe-jaxrs/profile/delete',
true);
request.withCredentials = true;
request.setRequestHeader("Content-type", "text/plain");
request.send(data);
</script>