关于springboot中的标签@Controller和@RestController

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fishman45/article/details/88392734

今天在做一个SpringBoot Demo的时候,用了@RestController,但是我想在该类中重定向到到自己的jsp页面,结果没有重定向成功,而是return到自己定义的内容。

当我将@RestController换成的@Controller时候,重定向重定向正确。

但出现了一个新的问题,就是不能将JSON内容返回到页面。此时因该在需要返回JSON的方法上添加@ResponseBody即可。

总结:

1、如果在Controller添加了@RestController注解,那么Controller层的方法中无法重定向到jsp或者html页面,其返回的内容就是自定义的内容。

2、如果在Controller添加了@Controller注解,那么Controller层的方法中能成功重定向到jsp或者html页面,但如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。

猜你喜欢

转载自blog.csdn.net/fishman45/article/details/88392734