本文主要是介绍@RestController的一些事,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
spring4.0以后出来的这个注解,简而言之就是@RestController = @Controller + @ResponseBody,这让我们既可以标注为controller层,也可以直接返回json数据
之前我们写代码是这样子的
@Controller
@ResponseBody
public class controllerTest { }
@RestController
public class restControllerTest { }
用完注解有一些疑问,返回变成json数据,那我们重定向或者转发怎么办?
搜集了如下解决方案
1、转发
@RequestMapping(value="/login", method=RequestMethod.POST)public ModelAndView login(){ModelAndView mv = new ModelAndView("index");return mv;}
2、重定向 @RestController
public class FooController {@RequestMapping("/foo")void handleFoo(HttpServletResponse response) throws IOException {response.sendRedirect("some-url");}
}
这篇关于@RestController的一些事的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!