SPRINGMVC the use of response and the method of resolving 500 null pointer exceptions cannot find response

Source: Internet
Author: User

When using annotations to load a request in a class (Controller), the request can be used normally (must be injected at startup, so hot deployment is not supported), but it is also used in this way to load a request already loaded Response Response 500 NULL pointer exception is not found

    @Autowired    httpservletrequest request;

Because both the request and the Response are for one

If only @autowired or @Resource annotations are used, the dependencies are injected when the app starts, and the request does not come when the app starts, so there are no requests and response objects.
So when the request and response objects are needed, they need to be placed in the parameters of the controller's method so that each time the request is requested, the Spring MVC framework automatically httpserveletrequest or Httpserveletresponse object injection.

The Request object represents a single call that contains all the information for this request, including the HTTP header and Body,
The response object represents the response to the request, and you can set the header and body of the response

However, if you want to use response, do not use @Autowired in the Class (Controller) to load the request or request and response in order to avoid collisions when the request and response are passed in the method , the latter is because the loaded response is not available, so use response in SPRINGMVC to meet the following conditions:

1. Do not use @Autowired to load request or request and response

2. Because @autowired does not support hot deployment, changes to request and response cannot be viewed in real time, so scenarios that need to be modified and viewed in real time do not use @Autowired to load request or request and response

3. You must also pass in the request and response in the method, like this

     Public Modelandview Showrepobyuidforpager (pageinfo<repoentity> pi,httpservletrequest request,HttpServletResponse Response) {

If the request is loaded with the @Autowired, the response injected with the method, or the request and the response are loaded with @Autowired, which will result in 500, NULL pointer exception, response not found, Therefore, both the request and the response must be passed in the method at the same time so that response and request can be used normally.

SPRINGMVC the use of response and the method of resolving 500 null pointer exceptions cannot find response

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.