Search code examples
springrestspring-mvcspring-restcontroller

Rest API call conflict with UrlBasedViewResolver in spring 4


I have a Spring MVC application which using JSP pages as view. This is how view resolver configured

<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/pages/" />
    <property name="suffix" value=".jsp" />
</bean>

Here when I to implement a rest API controller,

@RestController
public class APIController {

    @RequestMapping(value = "/register", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON)
    public String processOnlineCRM() {
        return "success";
    }

}

I'm getting below error each time I call the API.

<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Message</b> /crm/pages/success.jsp</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/7.0.90</h3></body></html>

Is there any way to recognize jsp calls and API calls in spring 4?


Solution

  • I got a simple solution for this, using MappingJackson2JsonView as object to model and view will response in JSON format.

    ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());
    modelAndView.addObject("status", "success");
    modelAndView.addObject("message", "Successfully Created the Customer");
    

    Still not the solution with view resolver.