jersey

init method in jersey jax-rs web service

微笑、不失礼 提交于 2019-12-07 08:38:46
问题 I'm new with jax-rs and have build a web service with jersey and glassfish. What I need is a method, which is called once the service is started. In this method I want to load a custom config file, set some properties, write a log, and so on ... I tried to use the constructor of the servlet but the constructor is called every time a GET or POST method is called. what options I have to realize that? Please tell, if some dependencies are needed, give me an idea how to add it to the pom.xml (or

Get client ip in Jersey 2.22.2

五迷三道 提交于 2019-12-07 07:39:01
问题 I am trying to acess the clients ip that are calling my rest server but I only get null as a respons. The webserver is running and I can access is from the web browser. I have tried with @Context HttpServletRequest And also with @Context ContainerRequest request request.getRequestHeader("HTTP_FORWARDED") //HTTP_X_FORWARDED //HTTP_CLIENT_IP But neither whit sucess, the response is null or blank. Setup Jersey v: 2.22.2 Grizzly v: 2.3.22 Java v: 8 Rest.java import javax.ws.rs.GET; import javax

How to setup a Jersey client with Jackson (2.x) provider to process a POST request

不想你离开。 提交于 2019-12-07 07:22:12
问题 I'm struggling to setup a Jersey client for testing a POST request to a resource. My Jersey and Jackson dependencies look like these: <dependencies> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-grizzly2-http</artifactId> </dependency> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.jaxrs</groupId> <artifactId>jackson-jaxrs-json

How can I send a generic file to a jersey service and receive it correctly?

送分小仙女□ 提交于 2019-12-07 07:10:25
问题 I'm developing a Jersey service that uses Dropbox's API. I need to post a generic file to my service (the service would be able to manage every kind of file as well as you can do with the Dropbox API). Client Side So, I've implemented a simple client that: opens the file, creates a connection to the URL, sets the correct HTTP method, creates a FileInputStream and writes the file on the connection's outputstream using byte buffer. This is the client test code. public class Client { public

HTTP Status 415 - Unsupported Media Type for AJAX call in JQUERY to Restful WS implemented with JERSEY

被刻印的时光 ゝ 提交于 2019-12-07 06:53:40
问题 Hi I am trying to post json data to Restful WS implemented with Jersey. I am posting data through jquery-ajax. Why am I geting HTTP Status-415 unsupported Media type? Thank you. Click here for screenshot of firebug description //post method handler @Path("/newentry") public class NewEntry { @POST @Consumes(MediaType.APPLICATION_JSON) public Response newEntry(String data) { //doStuff } } // ajax call $.ajax({ url: "http://localhost:8080/FirstRestWebService/rest/newentry", type: "post", data:

How to start a Jersey Test Container (Grizzly) once for all tests in a test class?

∥☆過路亽.° 提交于 2019-12-07 06:40:28
问题 I am working on fixing the integration tests in one of the projects. Currently, all the integration test classes extend the JerseyTest class. Going through the JerseyTest class I realized that it starts and stops the container for every test method using Junit's Before and After annotations. Why is this necessary? Isn't it enough if we bring up the container once, run the tests and shut it down at the end of it? We also use Spring and it takes time for the context to get initialized. Prior to

How to serialize a POJO into query params with Jersey

怎甘沉沦 提交于 2019-12-07 06:07:27
问题 I have been playing around and creating multiple small Java RESTful client libraries for different services at my company. Most of the time, I am unable to change anything on the server side and I need to write the Jersey pieces of code to interact with existing RESTful APIs. Context Up to know, I have been using Jersey with Jackson to use JSON: when I query a POJO I deserialize it from JSON, and when I need to send a POJO, I serialize it into a JSON body. This two kinds of snippets have been

How to mock MyBatis mapper interface?

好久不见. 提交于 2019-12-07 05:28:25
问题 I am writing unit test for my Jersey rest API which uses MyBatis at the background. This is the structure of my classes: rest service: @Path("/api") public class HelloRestService { @Inject HelloBean helloBean; @GET @Path("/echo/{name}") public Response echo(@PathParam("name") String name) { return Response.status(200).entity(helloBean.sayHello(name)).build(); } } Stateless EJB: @Stateless public class HelloStatelessBean implements HelloBean { // Injected MyBatis mapper (dao) @Inject private

Jersey (JAX-RS) how to map path with multiple optional parameters

坚强是说给别人听的谎言 提交于 2019-12-07 05:04:40
问题 I need to map path with multiple optional arguments to my endpoint path will look like localhost/func1/1/2/3 or localhost/func1/1 or localhost/func1/1/2 and this path should be correctly matched with public Double func1(int p1, int p2, int p3){ ... } What should I use in my annotations? It's test task to play with Jersey to find a way for using multiple optional params, not to learn REST design. 回答1: To solve this you need to make your params optional, but also / sign optional In the final

Jersey RESTful Services: Resources & Responses

微笑、不失礼 提交于 2019-12-07 04:53:09
问题 I see a lot of Jersey-based web services consisting of 1+ WebResources that have 1+ endpoints/methods like so: package com.myws.fizz; public class FizzResource { @GET @Path("/fizz/{id}") public Response getFizzById(@PathParam("id") Long id) { // ...etc. } @GET @Path("/fizz") public Fizz getFizzByFoo(Foo foo) { // ...etc. } } package com.myws.buzz; public class BuzzResource { @POST @Path("/buzz") public Response createBuzz(Buzz buzz) { // ...etc. } } I'm confused with what Jersey considers a