您的位置:首页 > 编程语言 > Java开发

SpringBoot入门(三)Controller的使用

2017-08-11 11:54 585 查看

浅析

@Controller:处理http请求

@RestController:Spring4之后新加的注解,用于代替原来@ResponseBody +@Controller的组合

@RequestMapping:配置url映射(用户通过访问某个url访问到我们某个方法)

@Controller

原来的代码

@RestController
public class HelloController {
@Autowired
private PersonProperties mPersonProperties;

@RequestMapping(value="/helloctrl", method=RequestMethod.GET)
public String say() {
return "name:"+mPersonProperties.getName()+"age:"+mPersonProperties.getAge();
}
}


使用@Controller(仅为了解。因为后面一般还是使用@RestController)

1、pom.xml中添加spring官方模板

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>


2、在src/main/resources/template文件夹下创建index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"></meta>
<title>Insert title here</title>
</head>
<body>
Hello Controller
</body>
</html>


3、修改@RestController为@Controller,修改返回值为index

@Controller
public class HelloController {
@Autowired
private PersonProperties mPersonProperties;

@RequestMapping(value="/helloctrl", method=RequestMethod.GET)
public String say() {
return "name:"+mPersonProperties.getName()+"age:"+mPersonProperties.getAge();
}
}


4、运行结果



@RestController=@Controller+@ResponseBody组合

@Controller
@ResponseBody
public class HelloController {
@Autowired
private PersonProperties mPersonProperties;

@RequestMapping(value="/helloctrl", method=RequestMethod.GET)
public String say() {
return mPersonProperties.getName();
}
}




@RequstMapping配置url



访问路径可以是

http://localhost:8088/controller/helloctrl

或者

http://localhost:8088/controller/hi

如何处理url中的参数

@PathVariable:获取url中的数据

@RequestParam:获取请求参数的值

@GetMapping:组合注解(简化@RequestMapping(value= {“/helloctrl”}, method=RequestMethod.GET)=@GetMapping(value= {“/helloctrl”}))

@PathVariable

@RestController
@RequestMapping("/controller")
public class HelloController {
@Autowired
private PersonProperties mPersonProperties;

@RequestMapping(value= {"/helloctrl/{id}"}, method=RequestMethod.GET)
public String say(@PathVariable("id") Integer id) {
return "id"+id;
}
}




还可以如下写:

@RequestMapping(value= {"/{id}/helloctrl"}, method=RequestMethod.GET)


访问地址

http://localhost:8088/controller/100/helloctrl


设置默认值

@RestController
@RequestMapping("/controller")
public class HelloController {
@Autowired
private PersonProperties mPersonProperties;

@RequestMapping(value= {"/helloctrl"}, method=RequestMethod.GET)
//required是否一定要传递值,设置为false,不需要
public String say(@RequestParam(value="id",required=false,defaultValue="10") Integer myid) {
return "id"+myid;
}
}




@GetMapping

@RestController
@RequestMapping("/controller")
public class HelloController {
@Autowired
private PersonProperties mPersonProperties;

@GetMapping(value="/helloctrl")
public String say(@RequestParam(value="id",required=false,defaultValue="20") Integer myid) {
return "id"+myid;
}
}


与@GetMapping相对应的还有@PostMapping、@PutMapping、@DeleteMapping
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: