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; }

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

How to inject an object into jersey request context?

You could just use ContainterRequestContext.setProperty(String, Object). Then just inject the ContainerRequestContext @Override public void filter(ContainerRequestContext crc) throws IOException { MyObject obj = new MyObject(); crc.setProperty(“myObject”, myObject); } @POST public Response getResponse(@Context ContainerRequestContext crc) { return Response.ok(crc.getProperty(“myObject”)).build(); } Another option to inject the MyObject directly is to use the HK2 functionality Jersey 2 offers. Create a … Read more

QueryParam binding with enum in jersey

From the Javadoc, the @QueryParam annotated type must either: Be a primitive type Have a constructor that accepts a single String argument Have a static method named valueOf or fromString that accepts a single String argument (see, for example, Integer.valueOf(String)) Be List<T>, Set<T> or SortedSet<T>, where T satisfies 2 or 3 above. The resulting collection … 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

How to download a file using a Java REST service and a data stream

“How can I directly (without saving the file on 2nd server) download the file from 1st server to client’s machine?” Just use the Client API and get the InputStream from the response Client client = ClientBuilder.newClient(); String url = “…”; final InputStream responseStream = client.target(url).request().get(InputStream.class); There are two flavors to get the InputStream. You can … Read more

@POST in RESTful web service

Please find example below, it might help you package jersey.rest.test; import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.HEAD; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.core.Response; @Path(“/hello”) public class SimpleService { @GET @Path(“/{param}”) public Response getMsg(@PathParam(“param”) String msg) { String output = “Get:Jersey say : ” + msg; return Response.status(200).entity(output).build(); } @POST @Path(“/{param}”) public Response … Read more

Swagger UI passing authentication token to API call in header

@ApiImplicitParams and @ApiImplicitParam should do the trick: @GET @Produces(“application/json”) @ApiImplicitParams({ @ApiImplicitParam(name = “Authorization”, value = “Authorization token”, required = true, dataType = “string”, paramType = “header”) }) public String getUser(@PathParam(“username”) String userName) { … } From the documentation: You may wish you describe operation parameters manually. This can be for various reasons, for example: Using … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)