Understanding How Spring MVC’s @RequestMapping POST Works

This is complicated, I think it is better to read the code. In Spring 3.0 The magic is done by method public Method resolveHandlerMethod(HttpServletRequest request) of the inner class ServletHandlerMethodResolver of org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter. An instance of this class exists for every Request Controller Class, and has a field handlerMethods that contains a list of all the … Read more

Proper way of streaming using ResponseEntity and making sure the InputStream gets closed

you can try to use StreamingResponseBody StreamingResponseBody A controller method return value type for asynchronous request processing where the application can write directly to the response OutputStream without holding up the Servlet container thread. Because you are working on a separate thread, writing directly to the response, your problem to call close() before return is … Read more

Spring mvc Ambiguous mapping found. Cannot map controller bean method

You should write @Controller(“/review”) public class ReviewController { and @Controller(“/book”) public class BookController { because in your code you have the two methods without an explicit/unique path for mapping(eg. if we have a call /edit/1 , this is impossible clearly to determine a controller’s method from your editBook BookController or ReviewController editReview)

Combine GET and POST request methods in Spring

@RequestMapping(value = “/testonly”, method = { RequestMethod.GET, RequestMethod.POST }) public ModelAndView listBooksPOST(@ModelAttribute(“booksFilter”) BooksFilter filter, @RequestParam(required = false) String parameter1, @RequestParam(required = false) String parameter2, BindingResult result, HttpServletRequest request) throws ParseException { LONG CODE and SAME LONG CODE with a minor difference } if @RequestParam(required = true) then you must pass parameter1,parameter2 Use BindingResult and request … Read more

Spring 3 MVC accessing HttpRequest from controller

Spring MVC will give you the HttpRequest if you just add it to your controller method signature: For instance: /** * Generate a PDF report… */ @RequestMapping(value = “/report/{objectId}”, method = RequestMethod.GET) public @ResponseBody void generateReport( @PathVariable(“objectId”) Long objectId, HttpServletRequest request, HttpServletResponse response) { // … // Here you can use the request and response … Read more

Spring 3 RequestMapping: Get path value

Non-matched part of the URL is exposed as a request attribute named HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE: @RequestMapping(“/{id}/**”) public void foo(@PathVariable(“id”) int id, HttpServletRequest request) { String restOfTheUrl = new AntPathMatcher().extractPathWithinPattern(request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE).toString(),request.getRequestURI()); … }

tech