Inject httpservletrequest

I am using a jersey In one of the methods of the jersey class I am trying to inject HttpServletRequest using Context annotation. If I make my class "ApplicationScoped" then its working fine. But that doesnt make a feasible fix because I would want it to be request scoped. Can anyone guide me on what could be the root cause of the issue?

Spring - Autowire - Dependency Injection - Spring Boot

Perhaps it's supposed to delegate to the CDI provided http artifact but anways it looks like it gets hit before the CDI contexts have been set up.

What jars are included in your deployment? As asked on cdi - Can not inject HttpServletRequest object using Context in jersey class in wildfly How did you install Jersey? I have included jersey-core, jersey-json, jersey-server and jersey-servlet in pom. All are versiond at 1. If you want Jersey to interact with the container you need to install it as a module and remove the current JAX-RS subsystem. I'm not sure exactly how this would work, but Jersey would need to be managed by the container if you want CDI to work.

Then the implementation won't matter.

inject httpservletrequest

Home Content Places Search Cancel. Error: You don't have JavaScript enabled. This tool uses JavaScript and much of it will not work correctly without it enabled. Please turn JavaScript back on and reload this page. Please enter a title. You can not post a blank message. Please type your message and try again. This project is read only now. Read more. I have the same question 0.

This content has been marked as final. Show 4 replies. Go to original post. Retrieving dataNeed support for your remote team? Check out our new promo! IT issues often require a personalized solution.

Why EE? Get Access. Log In. Web Dev. NET App Servers. We help IT Professionals succeed at work. Autowired HttpServletRequest vs passing as parameter to controller. Software Programmer asked. Medium Priority. Last Modified: Assume we have defined controller class which has Controller annotation only.

Inside the class we have defined private Autowired HttpServletRequest request; variable Spring Controllers are singleton. When we defined HttpServletRequest as Autowired in a web-application. I read from web-site that eventhough it is Autowired it just injects a proxy for the thread variable.

Is it true? In a multi-threaded environment can we use Autowired or passing HttpServletRequest as a parameter to each method in the controller class would be the right approach???

Start Free Trial. View Solutions Only. Distinguished Expert This award recognizes someone who has achieved high tech and professional accomplishments as an expert in a specific topic. Commented: It is not a good approach. Singletons are not thread safe and if there are instance variables in them then there will be problems in a multi-threaded environment.

Speaking specifically about the HttpServletRequest, it is not common to autowire it. Why do you need to do this? Software Programmer. Author Commented: You are saying it is not thread safe.The servlet container creates an HttpServletRequest object and passes it as an argument to the servlet's service methods doGetdoPostetc.

String changeSessionId Change the session id of the current session associated with this request and return the new session id. String getAuthType Returns the name of the authentication scheme used to protect the servlet. Cookie [] getCookies Returns an array containing all of the Cookie objects the client sent with this request. String getPathInfo Returns any extra path information associated with the URL the client sent when it made this request. String getPathTranslated Returns any extra path information after the servlet name but before the query string, and translates it to a real path.

String getRemoteUser Returns the login of the user making this request, if the user has been authenticated, or null if the user has not been authenticated.

HttpSession getSession Returns the current session associated with this request, or if the request does not have a session, creates one. Principal getUserPrincipal Returns a java.

[email protected]

Principal object containing the name of the current authenticated user. As of Version 2. All servlet containers support basic, form and client certificate authentication, and may additionally support digest authentication.

If the servlet is not authenticated null is returned. This method returns null if no cookies were sent. Use this method with headers that contain dates, such as If-Modified-Since.

The date is returned as the number of milliseconds since January 1, GMT. The header name is case insensitive. If the request did not have a header of the specified name, this method returns If the header can't be converted to a date, the method throws an IllegalArgumentException.

If the request did not include a header of the specified name, this method returns null. If there are multiple headers with the same name, this method returns the first head in the request.

You can use this method with any request header. Some headers, such as Accept-Language can be sent by clients as several headers each with a different value rather than sending the header as a comma separated list. If the request did not include any headers of the specified name, this method returns an empty Enumeration.

Parameters: name - a String specifying the header name Returns: an Enumeration containing the values of the requested header. If the request does not have any headers of that name return an empty enumeration. If the request has no headers, this method returns an empty enumeration.

If the request does not have a header of the specified name, this method returns If the header cannot be converted to an integer, this method throws a NumberFormatException.

This method returns null if there was no extra path information.

Spring Boot - Interceptor

If the URL does not have any extra path information, this method returns null or the servlet container cannot translate the virtual path to a real path for any reason such as when the web application is executed from an archive.

The web container does not decode this string. The context path always comes first in a request URI.GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. The field in the filter is always null. In Jersey 1. Can this be fixed? There are cases where there's value in accessing the HttpServletRequest and in turn the ServletRequest directly in the filter.

Some data points for example getting a remote address are not available in the ContainerRequestContext argument passed to the filter method. It is assigned to version 2. Skip to content. This repository has been archived by the owner. It is now read-only. Dismiss Join GitHub today GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. Sign up. Copy link Quote reply. The following code doesn't work in Jersey 2.

Affected Versions [2. This comment has been minimized. Sign in to view. Reported by iamtheotherian.

inject httpservletrequest

Marked as fixed on Friday, October 18tham. This issue was imported from java. This was referenced Apr 25, Cannot inject ContainerRequestContext to interceptor, reader, writerexception mapper. This was referenced Apr 20, Sign up for free to subscribe to this conversation on GitHub.And it was good.

I got the message "The injected service is com. SimpleCDIService b6. So it seems that Weld was able to inject into servlets under AS7 but not under Jetty 6.

HK2 is not injecting the HttpServletRequest with jersey

There may be a weld configuration problem in the CDI quickstart project. I'm not sure. I tried switching back to the standard weld-servlet listener org. Listener in web. If your app works properly with org. Listener instead of org. Jonathan, thanks for the reply. I noticed that we have following comment in web. So what is this "bean validation problems" that org. The reason why I am asking is because if I switch back to org. Listener, Guvnor failed to start using mvn gwt:run, and I found following errors in the log, and they seemed to be related to bean validation:.

Those aren't Bean Validation errors. They are plain Weld errors that mean what they mean -- that is if you're enabling Weld to manage all classes in your application classpath which is the default behaviour of Weld. If you're using a normal-scoped bean in CDI, you are required to make it proxyable.

You can't have an ApplicationScoped bean which has a private constructor. This is simply not permitted by the spec, because it means that it cannot be proxied to handle narrower-scope injection as well as cycle resolution. Just following up on this: have you tried creating a public constructor for org. It seems like that should solve the problem. Yes, the workaround using org. Though I am not sure what kind of bean validation problems I may run into if I switch back to use org.

We investigated this thoroughly yesterday, and have determined that the CDIServletStateListener is no longer necessary. I tracked down the history and found out it was forked from Weld 1. The only difference from the upstream Weld Listener class was that it skipped the validation phase at startup.

But since then, the official Weld version has changed a lot. For the 2. Final release, all demos, archetypes, and documentation will be using org. Home Content Places Search Cancel. Error: You don't have JavaScript enabled. This tool uses JavaScript and much of it will not work correctly without it enabled. Please turn JavaScript back on and reload this page. Please enter a title. You can not post a blank message.

Please type your message and try again.We use Weld with Tomcat web server. Dependency injection DI is a technique where one object supplies the dependencies of another object. In case of Java applications, it is a specific library that injects dependencies into the classes.

The major benefit of DI is loose coupling and ease of use. DI makes classes more cohesive because they have fewer responsibilities. It is standard for dependency injection and contextual lifecycle management. The Inject annotation is used to inject a dependency into a Java class. The dependency that is to be injected can be optionally decorated with ManagedBean annotation. In the following web application, we create a servlet that returns a list of city objects in an HTML file.

In the application, we use dependency injection with the help of the Weld library. This is the Maven POM file. The javax. The weld-servlet-shaded dependency enables Weld to be run in a Servlet container.

The jstl adds the JSTL library to the project.

inject httpservletrequest

The maven-war-plugin is responsible for collecting all artifact dependencies, classes and resources of the web application and packaging them into a web application archive WAR. In the Tomcat context. The City bean holds data of a city object. It has three attributes: idnameand population. It is a deployment descriptor for CDI. It can be used for configuring interceptors, decorators, and other things. Even if we do no configurations, we need to add an empty beans.

CityService contains the implementation of the ICityService interface. The service class calls the method of the DAO object, which is an intermediary layer to the database. ManagedBean is an optional annotation which indicates that the CityService is going to be managed by Weld.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service.

The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Attempting to inject it leads to issues because there are typically many implementations of the interface,and your CDI container does not have enough information to make a decision on which implementation to inject.

Even if you successfully injected an instance of it, it would not be the same instance as being managed by the EJB container. On the other hand, if you've got a more complex arrangement of dependency injection the question is certainly valid. This capability has been added to the CDI 1. In other words, the newest class of Java Enterprise Application servers will be able to handle this.

In the meantime, if you need to be able to manage some request scopes dependencies that in term need access to the actual HttpServletRequest information, you can use your approach and use the JBoss Solder tool.

Don't panic if the web site looks defunct, the fact of the matter is the work got shifted to the official CDI 1. But it's still perfectly suitable for CDI 1. Learn more. Asked 7 years, 5 months ago.

Active 4 years, 3 months ago. Viewed 18k times. Kirill Bazarov Kirill Bazarov 4, 5 5 gold badges 28 28 silver badges 48 48 bronze badges.

Active Oldest Votes. To acquire a properly managed instance of the request, do this instead: Context private HttpServletRequest httpRequest. Perception Perception Did you mean javax.

KirillBazarov - yes, thats the annotations to use. KirillBazarov - did this solve your problem? Therefore, this answer looks at first glance a completely blind guess and an ignorant shoot in the dark, and after all a lucky shot.

Nah, when you've been working a web service implementation for a year you kind of recognize when someone else is running into the same problems as you did early in the process. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password.


Comments

Leave a Comment

Your email address will not be published. Required fields are marked *