springboot的restcontroller
2017-02-25 17:51
639 查看
spring boot 基于spring MVC的基础上进行了改进, 将@Controller 与@ResponseBody 进行了合并成一个新的注解
@RestController。
当用户请求时,需要有视图渲染的,与请求数据的请求分别使用@Controller与@RestController 。
[java] view
plain copy
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);
}
}
请求数据:
[java] view
plain copy
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!";
}
}
返回数据:
[java] view
plain copy
Hello world!
如果返回类型的是一个 Class, value 是请求的映射集合, method是请求格式的集合,produces 是返回数据格式。
请求包含数据的视图:
[java] view
plain copy
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 页面。
@RestController。
当用户请求时,需要有视图渲染的,与请求数据的请求分别使用@Controller与@RestController 。
[java] view
plain copy
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);
}
}
请求数据:
[java] view
plain copy
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!";
}
}
返回数据:
[java] view
plain copy
Hello world!
如果返回类型的是一个 Class, value 是请求的映射集合, method是请求格式的集合,produces 是返回数据格式。
请求包含数据的视图:
[java] view
plain copy
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 Boot中@Controller和@restController的区别
- Springboot中"RestController cannot be resolved to a type"
- SpringBoot中常用注解@Controller/@RestController/@RequestMapping的区别
- 关于Spring-boot学习中对于@Controller和@RestController的简单记录
- spring-boot restcontroller filter
- Springboot中"RestController cannot be resolved to a type"
- Spring Boot的被@SpringBootApplication注解的App.java必须放在所有的RestController的根路径的package下
- 【spring Boot】Spring中@Controller和@RestController之间的区别
- SpringBoot 中常用注解@Controller/@RestController/@RequestMapping介绍
- spring boot 使用@RestController 和 @Controller 构造常见http get ,post请求例子
- SpringBoot 中常用注解@Controller/@RestController/@RequestMapping介绍(转)
- springBoot @RestController @Controller
- Spring Boot项目@RestController使用重定向redirect
- SpringBoot的RestController vs @ResponseBody + @Controller
- Spring Boot项目@RestController使用重定向redirect
- SpringBoot RestController 同时支持返回xml和json格式数据
- 在Spring Boot中输出REST资源
- SpringBoot系列(一)RestTemplate
- spring boot rest例子
- spring-boot系列一 之restfull api与多环境配置