Spring MVC @AutoWired answer doesn't work

I have:

@Controller
@RequestMapping(value="admin/*", method=RequestMethod.GET)
public class AdminController {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private HttpServletResponse response;

    @RequestMapping
    public ResponseEntity<String> test0() {
        System.out.println("ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ");
        return null;
    }


}

      

and the tag:

<mvc:annotation-driven />

      

in my config.xml

It should be enough for me, I feel, but there is a problem with @Autowired:

No bean match of type [javax.servlet.http.HttpServletResponse] found for dependency: at least 1 bean expected, which matches ...

I have seen a couple of solutions regarding setting up beans, etc., but I'm sure there must be some better way. Annotation-scanning should take care of this. It sucks if I need to configure beans in the xml for several different annotations at different times. I just want annotations to work when I use them!

I saw: Spring MVC is the answer

Thank!

+3


source to share


3 answers


As a workaround:

@RequestMapping
public ResponseEntity<String> test0(
        HttpServletRequest request, 
        HttpServletResponse response) {
    System.out.println("ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ");
    return null;
}

      



Also try adding RequestContextListener

, but it should be necessary in Spring MVC framework.

+3


source


Autoresponder does not work for answering, only request. There are workarounds, but they are kind of hacky and lame. I faced the same problem, here's my original question with a link to a workaround: @Autowired HttpServletResponse



+3


source


It doesn't work the way you want like fields, because the request and the repetition of changes after each request (in the absence of a better explanation). You cannot re-enter a new request / response each time in the fields. This is why you must add them to the method, where they will be inserted each time they are new.

+1


source







All Articles