Wojciech Kumoń
Wojciech Kumoń

Reputation: 325

Spring RequestMapping wrong view path

I have a problem with mappings and ModelAndView. All simply mappings like "/login" works but I have problem with:

@RestController
@RequestMapping("/note/{noteId:[\\d]+}")
public class NoteController {

  @RequestMapping(method = RequestMethod.GET)
  public ModelAndView note(@PathVariable String noteId, HttpSession session) {
    // not important code
    ModelAndView modelAndView = new ModelAndView("note");
    return modelAndView;
  }
}

I have an error here and path: /note/WEB-INF/views/jsp/note.jsp.

I wanted to get WEB-INF/views/jsp/note.jsp and I have similar path in other ModelAndViews (without prefix).

My configuration:

@Bean
public InternalResourceViewResolver viewResolver() {
  InternalResourceViewResolver resolver = new InternalResourceViewResolver();
  resolver.setPrefix("WEB-INF/views/jsp/");
  resolver.setSuffix(".jsp");
  return resolver;
}

Upvotes: 0

Views: 1056

Answers (1)

Wojciech Kumoń
Wojciech Kumoń

Reputation: 325

M. Deinum solved this problem.

Replace @RestController with @Controller

@RestController returns everything as a response body, it automatically returns your ModelAndView as JSON or XML. @Controller does proper view resolution. Also your prefix should be /WEB-INF/views/jsp/. Include the leading /.

Upvotes: 2

Related Questions