spring boot 处理请求的方式 Controller 与RestController
2016-11-04 13:28
567 查看
spring boot 基于spring MVC的基础上进行了改进, 将@Controller 与@ResponseBody 进行了合并成一个新的注解 @RestController。
当用户请求时,需要有视图渲染的,与请求数据的请求分别使用@Controller与@RestController 。
请求数据:
如果返回类型的是一个 Class, value 是请求的映射集合, method是请求格式的集合,produces 是返回数据格式。
请求包含数据的视图:
页面会跳转到 对应的 demo.html 或者 demo.jsp 页面。
下一章讲用户配置的几种模板支持。
当用户请求时,需要有视图渲染的,与请求数据的请求分别使用@Controller与@RestController 。
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Hello world! */ //其中@SpringBootApplication申明让spring boot自动给程序进行必要的配置,等价于以默认属性使用@Configuration,@EnableAutoConfiguration和@ComponentScan @SpringBootApplication publicclassApp { publicstatic void main(String[] args) { SpringApplication.run(App.class, args); } }
请求数据:
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController// 标记为:restful public class HelloController { @RequestMapping(value={"/",""},method={RequestMethod.POST,RequestMethod.GET},produces = "application/json; charset=UTF-8") public String hello(){ return"Hello world!"; } }返回数据:
Hello world!
如果返回类型的是一个 Class, value 是请求的映射集合, method是请求格式的集合,produces 是返回数据格式。
请求包含数据的视图:
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/demo") public class DemoController { @RequestMapping("/getDemo") public String getDemo() { Map map=new HashMap(); map.put("key","value"); return "demo"; } }
页面会跳转到 对应的 demo.html 或者 demo.jsp 页面。
下一章讲用户配置的几种模板支持。
相关文章推荐
- Spring处理跨域请求(含有SpringBoot方式)
- Spring MVC Controller与jquery ajax请求处理json
- 009,spring boot web 前端跨域请求处理
- spring boot 拦截器 或 Spring AOP 方式记录请求日志
- Spring Boot :Request请求处理流程
- Spring Boot中使用AOP统一处理Web请求日志
- Spring控制器响应(action)请求的几种处理方式
- Spring Boot中使用AOP统一处理Web请求日志
- spring boot中的静态资源加载处理方式
- @Aspect统一处理Web请求日志--Spring Boot--Java EE开发,原来可以这样!
- Spring Boot中使用AOP统一处理Web请求日志
- spring boot get和post请求,以及requestbody为json串时候的处理
- Spring Boot如何让某个Controller支持跨源请求,以及如何让Controller类某个成员方法支持跨源请求
- Spring Boot自定义错误页面,Whitelabel Error Page处理方式
- Spring Boot中使用AOP统一处理Web请求日志
- Spring控制器响应(action)请求的几种处理方式,如返回JSON,MODEL MODELVIEW,STRING
- Spring Boot(十一)使用AOP,@Aspect统一处理Web请求日志
- 46. Spring Boot中使用AOP统一处理Web请求日志【从零开始学Spring Boot】
- Spring Boot中使用AOP统一处理Web请求日志
- spring boot 使用@RestController 和 @Controller 构造常见http get ,post请求例子