春季4中Rest API调用与UrlBasedViewResolver发生冲突

时间:2019-01-07 06:33:20

标签: spring rest spring-mvc spring-restcontroller

我有一个使用JSP页面作为视图的Spring MVC应用程序。 这是视图解析器的配置方式

<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>

在这里我要实现一个REST API控制器,

@RestController
public class APIController {

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

}

每次调用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>

在Spring 4中有什么方法可以识别jsp调用和API调用吗?

1 个答案:

答案 0 :(得分:0)

我有一个简单的解决方案,使用MappingJackson2JsonView作为对象进行建模和查看将以JSON格式进行响应。

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

仍然不是视图解析器的解决方案。