jersey

用jersey2搭建restful平台时遇到的问题

霸气de小男生 提交于 2020-01-07 08:18:50
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 前几天用jersey2搭建了一个restful平台,今天在测试的时候发现一个问题,如果一个没有权限的用户访问了限制资源,那么正确的返回结果应该为401。问题就在这里发生了,我写了一个测试用例,第一次访问的时候返回401,再次请求时就成了200,往后再怎么试都是200,除非重启应用。看看了日志发现报错了: An I/O error has occurred while writing a response message entity to the container output stream. ! java.lang.IllegalStateException: The output stream has already been closed. 看到了,当服务端往请求的客户端通道中返回数据时,发现客户端的通道已经关闭了。哦,明白了,看看了代码,发现问题的所以,如下: final Response ACCESS_DENIED = Response.status(Response.Status.UNAUTHORIZED).entity("无权限访问!").build(); 起初这段代码是做filter的类变量而使用的,也就是说定义了一个Response重复在使用,这样做是不对的,随后把改成了局部变量

Jersey Json mapping with java.lang.NullPointerException, Unable to Produce JSON

一曲冷凌霜 提交于 2020-01-07 06:05:46
问题 I have a domain class with @XMLRootElement annotation which will be return from the service. My service is something like: @GET @Produces(MediaType.APPLICATION_JSON) @Path("/GetProductList") public Response getProductList(@QueryParam("Username") String username, @QueryParam("Password") String password) { CategoryResponse result = new CategoryResponse(returnDummyCategories()); return Response.ok().entity(result).build(); } catch (Exception e) { log.error("Error fetching products.", e); throw

Designing REST resource to support multiple methods which produces and consumes exactly the same mime-types

人走茶凉 提交于 2020-01-07 05:35:25
问题 I have 3 methods in ProfileResource : @GET @Produces(MediaType.APPLICATION_JSON) public List<Profile> getAllProfiles() { return profileService.getAllProfiles(); } @GET @Path("{profileId}") @Produces(MediaType.APPLICATION_JSON) public Profile getProfile(@PathParam("profileId") String profileId) { return profileService.getProfile(profileId); } @GET @Produces(MediaType.APPLICATION_JSON) public Profile getProfileByName(@QueryParam("profileName") String profileName) { return profileService

JAX-RS client Jersey Framework required jars

こ雲淡風輕ζ 提交于 2020-01-07 03:34:08
问题 What minimum JAR files of Jersey Framework are needed to run a client? If I include all JAR's it will take 4 MB. 回答1: Jersey 2.x (2.22.1) <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-client</artifactId> <version>2.22.1</version> </dependency> Jersey 1.x (1.19) <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.19</version> </dependency> Note: These are just the base client jars. There is no JSON support. For JSON

HTTP Status 500 - org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]

China☆狼群 提交于 2020-01-07 02:50:28
问题 i run my hibernate getArray function in my main class and it run successfully but when i use the same method in my jersey rest api it gives me: HTTP Status 500 - org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml] i tried to put the method in my main class and run it and it run successfully then i tried to call it from the resource class and it didn't work! any clues here guys? This is my directory My Directory image And this is how

Jersey, Guice using non-root request paths

流过昼夜 提交于 2020-01-06 14:19:13
问题 I'm using Jersey 1.11 over Guice 3.0 on Tomcat 6.0.32 in a standard configuration: configureServlets() { filter("/ws/*").through(GuiceContainer.class); } And a simple resource class: @Path("/resource") public class Resource { ... } Given that, I would suppose that accessing "/ws/resource" would work; but actually no resources are found. The problem seems to lie in the request path not being computed correctly. As a workaround I have set the parameter PROPERTY_FILTER_CONTEXT_PATH to /ws ,

jersey cross domain request

亡梦爱人 提交于 2020-01-06 06:53:30
问题 I am using jersey 2.18 for developing rest api. (using tomcat container) I want to allow access to clients from other domain. So I am trying below code to allow cross domain requests. Filter public class MyCorsFilter implements Filter { public MyCorsFilter() { } public void init(FilterConfig fConfig) throws ServletException { } public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { (

Jersey 2 - ContainerRequestFilter get method annotation

☆樱花仙子☆ 提交于 2020-01-06 02:46:08
问题 Im trying to get the Method annotation in ContainerRequestFilter object. Controler: @GET @RolesAllowed("ADMIN") public String message() { return "Hello, rest12!"; } ContainerRequestFilter : @Provider public class SecurityInterceptor implements javax.ws.rs.container.ContainerRequestFilter { @Override public void filter(ContainerRequestContext requestContext) { //Here I need To get the @RolesAllowed("ADMIN") annotation value } Application : @ApplicationPath("/rest") public class ExpertApp

parsing subnodes with Jersey

做~自己de王妃 提交于 2020-01-06 02:23:08
问题 We are connecting to a third party using Jersey. We then want to extract the returned xml into our class. This is actually working fine except for one node in the xml that is in a subnode. Here is the xml returned: <response> ... <langISO>en</langISO> <acquirerAmount>1000</acquirerAmount> <acquirerCurrency>GBP</acquirerCurrency> <subXml> <authCode>122958</authCode> </subXml> </response> Note that the authCode node is in a subnode (called subXml). OurResponse myriadResponse = response

@XmlElementWrapper with a generic List and inheritance

旧街凉风 提交于 2020-01-05 15:06:07
问题 I'd like to do the following: public abstract class SomeItems<E> { protected List<E> items; public void setItems(List<E> items) { this.items = items; } ....do other stuff with items.... } @XmlRootElement(name = "foos") public class Foos extends SomeItems<Foo> { @XmlElementWrapper(name="items") @XmlElement(name="foo") public List<Foo> getItems() { return this.items; } } @XmlRootElement(name = "bars") public class Bars extends SomeItems<Bar> { @XmlElementWrapper(name="items") @XmlElement(name=