0

we have recently started migrating our web application from spring to spring boot 2.4.5. In spring we were able to send ModelAndView as a result from spring controller in Ajax call from UI, but after migrating to spring boot 2.4.5 we are not able to return ModelAndView in ajax call, getting 404 HTTP error.

Can anyone please help me with this, I have missed something during migration?

Thanks

sample code, which was working fine with spring but breaking in spring boot:

public ModelAndView saveData(HttpServletRequest request, @RequestParam final Map<String, String> map) { 
ModalAndView mav = new ModelAndView();
modelAndView.addObject("status"), "IS_WORKING");
return mav;
}

If i update the above code with below, then the ajax call is working fine witn spring boot:

public @ResponseBody ModelMap saveData(HttpServletRequest request, @RequestParam final Map<String, String> map) 
{
        ModelMap modelMap = new ModelMap();
       modelMap .addAttribute("status"), "IS_WORKING");
       return modelMap ;
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Surendra
  • 21
  • 4
  • 1
    Do you have some example code? Are you using `@Controller` or `@RestController`? Why do you think you can't return a `ModalAndView` ? – Wim Deblauwe Jun 14 '21 at 14:41
  • i am using @Controller and api is returning ModalAndView. I am not thinking that i can't return it indeed i am not able to receive ModalAndView if api is being called via Ajax call. – Surendra Jun 14 '21 at 14:57
  • updated the post with sample code. – Surendra Jun 14 '21 at 15:05

2 Answers2

0

To return only data to front end then use Modelmap. The ModelAndView will used for return values and view together at the same time by controller.

S. Anushan
  • 728
  • 1
  • 6
  • 12
  • It was working with spring but not with spring boot, is anything changed in spring boot with respect to the ajax call? I am concerned abt it bcoz this needs code change in lots places. – Surendra Jun 14 '21 at 17:27
0

I cannot tell you why it worked like it did in older Spring versions, but the best way forward will be to use the framework as intended:

  1. Use @RestController instead of @Controller. If you want to build a REST API with Spring, that is the best since there would be no need to add @ResponseBody on each method in your controller. @Controller is used if you want to use a templating engine like Thymeleaf for doing Server Side Rendering of HTML.
  2. You can return any Java object and it is serialized to JSON automatically (using Jackson under the hood). If you want to return ad-hoc data without creating a dedicated response DTO, then just return a java.util.Map instance:
Map<String,Object> response = new HashMap<>();
response.put("status", "IS_WORKING");
return response;
Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211
  • Thanks for the reply Wim. I enable the spring debug logs and found that in case if i do not give view name in ModalAndView(first sample code) then the spring api name(in my case saveData) is being taken as view name and then it is try to forward to that view, which it is not egtting and giving 404 error. (ContentNegotiatingViewResolver.java:349) DEBUG Selected '*/*' given [*/*] (ContentNegotiatingViewResolver.java:349) DEBUG Selected '*/*' given [*/*] (AbstractView.java:309) DEBUG View name 'saveData', – Surendra Jun 15 '21 at 12:47