users@jersey.java.net

Re: [Jersey] API cannot recognize method correctly

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 31 Mar 2010 10:15:14 +0200

On Mar 26, 2010, at 3:51 PM, KANISHK PANWAR wrote:

> Thanks for the response! Due to internal restrictions, the highest
> version that I can update to is 1.1.4.1 :((
>

OK. They you are some what stuck using the approach you have
presented :-(

I recommend that the @GET method return an instance of StreamingOutput:

   https://jsr311.dev.java.net/nonav/javadoc/javax/ws/rs/core/StreamingOutput.html

which can write directly to the output stream.

Paul.

> Here's what I noted. I tried using same HttpContext as a method
> argument and it failed again with same error message. Given below is
> the modified code.
> @GET
> @Path("/test")
> public void test(@Context HttpContext context) {
>
> try {
> PrintWriter writer = new PrintWriter(context.getResponse()
> .getOutputStream());
> writer.write("this is a test - " + new Date());
> writer.close();
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
>
>
> Also, it seems that my code is deployed as a servlet. Given below is
> the configuration from web.xml
>
> <servlet>
> <servlet-name>Jersey Web Application</servlet-name>
> <servlet-
> class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-
> class>
> <init-param>
> <param-name>com.sun.jersey.config.property.packages</
> param-name>
> <param-value>com.rightmedia.sarq.web.rest</param-value>
> </init-param>
> <load-on-startup>1</load-on-startup>
> </servlet>
>
> <servlet-mapping>
> <servlet-name>Jersey Web Application</servlet-name>
> <url-pattern>/resources/*</url-pattern>
> </servlet-mapping>
>
>
> Not sure, if I am deploying it correctly.
>
> thanks,
> Kanishk
>
> > Date: Thu, 25 Mar 2010 12:09:43 +0100
> > From: Paul.Sandoz_at_Sun.COM
> > To: users_at_jersey.dev.java.net
> > Subject: Re: [Jersey] API cannot recognize method correctly
> >
> > Hi Kanishk,
> >
> > It is a bug in the version of Jersey you are using. Try upgrading to
> > version 1.1.5.
> >
> > Injection of HttpServletRequest, HttpServletResponse, ServletContext
> > and ServletConfig is supported. The latter will only work if you
> > deploy as a servlet, otherwise if you deploy as a filter you need to
> > use FilterConfig.
> >
> > Paul.
> >
> > On Mar 24, 2010, at 11:06 PM, Kanishk Panwar wrote:
> >
> > > Fellow Users,
> > >
> > > I am using jersey 1.0.3 and I was experimenting with Injections
> when I
> > > got this error.
> > >
> > > First up I used the code below and it worked correctly.
> > > @Path("/v1")
> > > public class V1Service {
> > > @Context
> > > HttpContext context;
> > >
> > > @GET
> > > @Path("/test")
> > > public void test() {
> > > OutputStream os = null;
> > >
> > > try {
> > > os = context.getResponse().getOutputStream();
> > > os.write(("this is a test - " + new Date()).getBytes());
> > > os.close();
> > > } catch (IOException e) {
> > > e.printStackTrace();
> > > }
> > > }
> > > }
> > >
> > >
> > > When I made the modifications [shown below] I got this error.
> > > com.sun.jersey.api.container.ContainerException: Method, public
> void
> > > com.test.V1Service.test(javax.servlet.http.HttpServletResponse),
> > > annotated with GET of resource, class com.test.V1Service, is not
> > > recognized as valid Java method annotated with @HttpMethod.
> > >
> > > com
> > > .sun
> > > .jersey
> > > .server
> >
> >
> .impl.model.method.ResourceHttpMethod.<init>(ResourceHttpMethod.java:
> > > 92)
> > >
> > > com
> > > .sun
> > > .jersey
> > > .server
> >
> >
> .impl.model.method.ResourceHttpMethod.<init>(ResourceHttpMethod.java:
> > > 69)
> > >
> > > com
> > > .sun
> > > .jersey
> > > .server
> > > .impl
> >
> > .model.ResourceClass.processSubResourceMethods(ResourceClass.java:
> 258)
> > >
> > > com
> > > .sun
> >
> > .jersey.server.impl.model.ResourceClass.<init>(ResourceClass.java:
> 128)
> > >
> > > com
> > > .sun
> > > .jersey
> > > .server
> > > .impl
> > > .application
> > > .WebApplicationImpl.newResourceClass(WebApplicationImpl.java:348)
> > >
> > > com
> > > .sun
> > > .jersey
> > > .server
> > > .impl
> > > .application
> > > .WebApplicationImpl.getResourceClass(WebApplicationImpl.java:321)
> > >
> > > com
> > > .sun
> > > .jersey
> > > .server
> > > .impl
> > > .application
> > > .WebApplicationImpl.processRootResources(WebApplicationImpl.java:
> 807)
> > >
> > > com
> > > .sun
> > > .jersey
> > > .server
> > > .impl
> > > .application.WebApplicationImpl.initiate(WebApplicationImpl.java:
> 590)
> > >
> > > com
> > > .sun
> > > .jersey
> > > .server
> > > .impl
> > > .application.WebApplicationImpl.initiate(WebApplicationImpl.java:
> 383)
> > >
> > > com
> > > .sun
> > > .jersey
> > > .spi
> >
> > .container.servlet.ServletContainer.initiate(ServletContainer.java:
> > > 377)
> > > com.sun.jersey.spi.container.servlet.ServletContainer
> > > $InternalWebComponent.initiate(ServletContainer.java:242)
> > >
> > > com
> > > .sun
> >
> > .jersey.spi.container.servlet.WebComponent.load(WebComponent.java:
> 449)
> > >
> > > com
> > > .sun
> >
> > .jersey.spi.container.servlet.WebComponent.init(WebComponent.java:
> 169)
> > >
> > > com
> > > .sun
> > > .jersey
> >
> > .spi.container.servlet.ServletContainer.init(ServletContainer.java:
> > > 281)
> > >
> > > com
> > > .sun
> > > .jersey
> >
> > .spi.container.servlet.ServletContainer.init(ServletContainer.java:
> > > 442)
> > > javax.servlet.GenericServlet.init(GenericServlet.java:212)
> > >
> > > org
> > > .apache
> > > .catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:
> 102)
> > >
> > > org
> >
> > .apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:
> > > 286)
> > >
> > > org
> >
> > .apache.coyote.http11.Http11Processor.process(Http11Processor.java:
> > > 845)
> > > org.apache.coyote.http11.Http11Protocol
> > > $Http11ConnectionHandler.process(Http11Protocol.java:583)
> > > org.apache.tomcat.util.net.JIoEndpoint
> $Worker.run(JIoEndpoint.java:
> > > 447)
> > > java.lang.Thread.run(Thread.java:637)
> > >
> > >
> > > Given below is the code that generated the error.
> > > @Path("/v1")
> > > public class V1Service {
> > > @GET
> > > @Path("/test")
> > > public void test(@Context HttpServletResponse response) {
> > > OutputStream os = null;
> > >
> > > try {
> > > os = response.getOutputStream();
> > > os.write(("this is a test - " + new Date()).getBytes());
> > > os.close();
> > > } catch (IOException e) {
> > > e.printStackTrace();
> > > }
> > > }
> > > }
> > >
> > >
> > > Any ideas why this is not working correctly? Also, I would like to
> > > eventually use HttpServletResponse, HttpServletRequest and
> > > ServletConfig as injections to execute certain logic. Is that a
> > > possibility?
> > >
> > > Any input is greatly appreciated.
> > >
> > > Thanks,
> > > Kanishk
> > >
> > >
> ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> > > For additional commands, e-mail: users-help_at_jersey.dev.java.net
> > >
> >
> >
> >
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> > For additional commands, e-mail: users-help_at_jersey.dev.java.net
> >