Custom response header Jersey/Java

I think using javax.ws.rs.core.Response is more elegant and it is a part of Jersey. Just to extend previous answer, here is a simple example: @GET @Produces({ MediaType.APPLICATION_JSON }) @Path(“/values”) public Response getValues(String body) { //Prepare your entity Response response = Response.status(200). entity(yourEntity). header(“yourHeaderName”, “yourHeaderValue”).build(); return response; }

How to set and check cookies wih JAX-RS?

You can do the following: To store a new cookie: @GET @Path(“/login”) @Produces(MediaType.TEXT_PLAIN) public Response login() { NewCookie cookie = new NewCookie(“name”, “123”); return Response.ok(“OK”).cookie(cookie).build(); } To retrieve the cookie (javax.ws.rs.core.Cookie): @GET @Path(“/foo”) @Produces(MediaType.TEXT_PLAIN) public Response foo(@CookieParam(“name”) Cookie cookie) { if (cookie == null) { return Response.serverError().entity(“ERROR”).build(); } else { return Response.ok(cookie.getValue()).build(); } } However, … Read more

WebApplicationException vs Response

Why to use one possibility over the other since the result is the same? Maybe because as a (Java) programmer you are accustomed with throwing exceptions when particular rules of the application are broken? Convert some string to a number and you might get a NumberFormatException, use a wrong index in an array and you … Read more

Rest – how get IP address of caller

Inject a HttpServletRequest into your Rest Service as such: import javax.servlet.http.HttpServletRequest; @GET @Path(“/yourservice”) @Produces(“text/xml”) public String activate(@Context HttpServletRequest req,@Context SecurityContext context){ String ipAddressRequestCameFrom = requestContext.getRemoteAddr(); // header name is case insensitive String xForwardedForIP = req.getHeader(“X-Forwarded-For”); // if xForwardedForIP is populated use it, else return ipAddressRequestCameFrom String ip = xForwardedForIP != null ? xForwardedForIP : ipAddressRequestCameFrom; … Read more

FileUpload with JAX-RS

On Server Side you can use something like this @POST @Path(“/fileupload”) //Your Path or URL to call this service @Consumes(MediaType.MULTIPART_FORM_DATA) public Response uploadFile( @DefaultValue(“true”) @FormDataParam(“enabled”) boolean enabled, @FormDataParam(“file”) InputStream uploadedInputStream, @FormDataParam(“file”) FormDataContentDisposition fileDetail) { //Your local disk path where you want to store the file String uploadedFileLocation = “D://uploadedFiles/” + fileDetail.getFileName(); System.out.println(uploadedFileLocation); // save it … Read more

Jersey Grizzly REST service not visible outside localhost

To make a server IP adress visible outside of localhost, you must fist open the neccessary firewall ports(if you have one), or use “0.0.0.0” instead of “localhost” in order for the server to listen to all IP addresses and network adapters. Before testing it in your local network, try pinging your server device from your … Read more

Using @Context, @Provider and ContextResolver in JAX-RS

I don’t think there’s a JAX-RS specific way to do what you want. The closest would be to do: @Path(“/something/”) class MyResource { @Context javax.ws.rs.ext.Providers providers; @GET public Response get() { ContextResolver<StorageEngine> resolver = providers.getContextResolver(StorageEngine.class, MediaType.WILDCARD_TYPE); StorageEngine engine = resolver.get(StorageEngine.class); … } } However, I think the @javax.ws.rs.core.Context annotation and javax.ws.rs.ext.ContextResolver is really for types … Read more

What is the proper replacement of the Resteasy 3.X PreProcessInterceptor?

RESTEasy 3.x.x conforms to the JAX-RS 2.0 specification. What you are trying to do could be accomplished (maybe better) with: @Provider public class SecurityInterceptor implements javax.ws.rs.container.ContainerRequestFilter { @Override public void filter(ContainerRequestContext requestContext){ if (not_authenticated){ requestContext.abortWith(response)}; } } since the ReaderInterceptor is invoked only if the underlying MessageBodyReader.readFrom is called by the standard JAX-RS pipeline, not … Read more

When to use pathParams or QueryParams [duplicate]

My personal rule of thumb that the PathParam leads upto the entity type that you are requesting. /Invoices // all invoices /Invoices?after=2011 // a filter on all invoices /Invoices/52 // by 52 /Invoices/52/Items // all items on invoice 52 /Invoices/52/Items/1 // Item 1 from invoice 52 /Companies/{company}/Invoices?sort=Date /Companies/{company}/Invoices/{invoiceNo} // assuming that the invoice only unq … Read more

tech