简体   繁体   中英

RESTEASY003880: Unable to find contextual data of type: javax.servlet.http.HttpServletRequest

Hi when i am trying to open my applications' login page on browser , i am getting this exception. PS : open jdk zulu 11 and wildfly 24 for deployment.

org.jboss.resteasy.spi.LoggableFailure: RESTEASY003880: Unable to find contextual data of type: javax.servlet.http.HttpServletRequest
    at org.jboss.resteasy.resteasy-jaxrs@3.15.1.Final//org.jboss.resteasy.core.ContextParameterInjector$GenericDelegatingProxy.invoke(ContextParameterInjector.java:77)
    at javax.servlet.api@2.0.0.Final//com.sun.proxy.$Proxy163.getSession(Unknown Source)
    at deployment.demo.ear.rest.war//com.demoapp.demo.rest.resources.Myclass.init(Myclass.java:107)

Myclass.java

    import javax.ws.rs.core.Context;
    import javax.servlet.http.HttpServletRequest;

@Path("")
@Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.WILDCARD})
    public abstract class Myclass{
          @Context
        protected HttpServletRequest requestContext;
       
    
        @PostConstruct
        void init() {
            HttpSession session = requestContext.getSession();   // exception at this line
           
            try {
            
               //some code here
            } catch (Exception e) {
                
            }
        }
    }

Please suggest.

In my experience the @Context is set on a per method basis. More like:

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response myMethod(String someString, @Context HttpServletRequest request) {

In this way the HttpServletRequest is set on a method basis, not a class basis. I don't think you want it at the class level as if the class has many methods those methods shouldn't share the same HttpServletRequest .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM