jersey

Error handling in REST API with JAX-RS

吃可爱长大的小学妹 提交于 2019-12-10 01:10:15
问题 The task: Instead of receiving general HTTP 500 Internal Server Error in my stacktrace and the same horrible stacktrace on the client side I want to see my customized message with another statuscode ( 403 for example), that it will be much clearer for the developer, what has happend. And add some message to User about the Exception. Here are couple of changed classes from my application: SERVER PART: AppException.class - all my Server Response exceptions (before giving back to client) I want

Getting resource annotations in Jersey 1.18.1 request filter

纵饮孤独 提交于 2019-12-10 00:10:23
问题 I'm implementing a user authorization module that will be applied on a resource method using a (new) annotation. In order to do so, I created a Jersey (request) filter in which I need to get the annotation in order to allow / disallow the resource operation. I'm using Dropwizard 0.7.1 with Jersey 1.18.1 The resource class: @Path("/v1/users/registration") @Produces(MediaType.APPLICATION_JSON) @Api(value = "/users/registration") public class UserRegistrationResource { @POST @AuthorizedFor(Realm

jersey (+ jackson) map field serialization

笑着哭i 提交于 2019-12-09 21:57:20
问题 i have a simple jersey web service and i'd like to consume / produce objects that contain map fields, like @XmlElement private Map<String,String> properties; if this string goes into the web service, { properties: { key1: val1, key2: val2 )} the properties field is deserialized as null with no errors. the same JSON goes in and out of GSON no problems, and in the short term i solved this by having jersey consume produce strings and using GSON to serialize / deserialize the JSON. any ideas?

Hibernate Error java.lang.NoSuchMethodError

匆匆过客 提交于 2019-12-09 19:57:50
问题 I'm using Jersey as Rest web service and Hibernate entity manager to persist JPA models. and I'm using Tomcat 8 as container. Here is the content of the persistence.xml file : <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <persistence-unit name="manager1" transaction-type="JTA"> <properties> <!

Nested resources in Jersey/JAX-RS -how to implement Restangular example

戏子无情 提交于 2019-12-09 18:15:15
问题 Thus may not actually be called nested resources from a Rest perspective, but I am interested in how to structure a Jersey class as a rest provider, so it can respond to chained requests. i.e I am ok with the basic /users, I am ok with /users/123 to get a specific user, but how to then branch down to properties of the user.... /users/123/cars, /users/123/cars/23 etc. Sorry for the lack of information, but saw this as an example in the Restangular documentation for Angular. https://github.com

Understanding REST APIs - What are Context and @Context?

房东的猫 提交于 2019-12-09 18:11:30
问题 I recently went through restful web services tutorial, but couldn't understand what a context is. Can someone explain what it it and also what @Contex t does? 回答1: JAX-RS provides the @Context annotation to inject 12 object instances related to the context of the HTTP request and they are: SecurityContext - Security context instance for the current HTTP request Request - Used for setting precondition request processing Application , Configuration , and Providers -> Provide access to the JAX

Call 1 jersey resource class from another Jersey resource class with @Context ServletContext

巧了我就是萌 提交于 2019-12-09 16:39:35
问题 I have Jersey resource class A calling a method in resource class B.Both classes have a @Context ServletContext servletContext at the class level. When I instantiate class B to call it from resource class A using its empty constructor, servletContext is null in the class B method being called. Is there any Jersey framework way I can call class B and yet have the servletContext retain its values/attributes from class A. 回答1: You can instantiate class B using ResourceContext. I.e. in class A

Jersey unable to catch any Jackson Exception

a 夏天 提交于 2019-12-09 16:02:26
问题 For my REST api I'm using jersey and ExceptionMapper to catch global exceptions. It works well all the exception my app throws but I'm unable to catch exception thrown by jackson. For example one of my endpoint accept an object that contains an enum. If the Json in the request has a value that is not in the enum jersey throw this exception back Can not construct instance of my.package.MyEnum from String value 'HELLO': value not one of declared Enum instance names: [TEST, TEST2] at [Source:

Jersey 2.0 “Getting Started” guide, mainClass not found

北城以北 提交于 2019-12-09 15:57:04
问题 Hi I am trying to follow the Getting Started guide for Jersey 2.0. I did steps 1.1 and 1.2 as is. No problem there. For step 1.3 I had a problem cause maven could not find the javax-annotation 1.2 but I solved it following the advice of another Stackoverflow user and added a repository to my pom. So mvn clean test passes with no problems, BUT when I try to run mvn clean exec:java I get back [WARNING] java.lang.ClassNotFoundException: com.example.Main at java.net.URLClassLoader$1.run

Jersey: Returning 400 error instead of 500 when given invalid request body

非 Y 不嫁゛ 提交于 2019-12-09 14:06:09
问题 I'm using Jersey's integrated Jackson processing to transform incoming JSON to a POJO, e.g.: @POST @Consumes(MediaType.APPLICATION_JSON) public Response newCustomer( CustomerRepresentation customer) { ... } If a client sends JSON with invalid fields Jersey currently returns a 500 Internal Server Error . Instead, I'd like to return a 400 Bad Request , preferably with some meaningful detail indicating which fields are in error. Any insight into how this could be accomplished? (At least