@RestController public class HelloController { @RequestMapping(value = "/hello", method = RequestMethod.GET) public String hello_world() { return "Hello World"; } }
在resources/application.properties中写入配置
server.port=5000
server.servlet.context-path=/front
eg: http://127.0.0.1:5000/front/hello
配置文件还可以是application.yml
server: port: 5000 servlet: context-path: /front
age: 18
@Value("${age}") private Integer age;
girl: gradeNum: B age: 18 content: "gradeNum: ${girl.gradeNum}, age: ${girl.age}"
这里再创建一个类GirlProperties
@Component @ConfigurationProperties(prefix = "girl") public class GirlProperties { private String gradeNum; private Integer age; private String content; getter setter省略 } @Autowired private GirlProperties girlProperties;
创建新的application-xxx.yml
application.yml的内容:
spring:
profiles:
active: xxx
当使用java -jar 运行时,可以添加参数选择配置文件
java -jar xxx-0.0.1-SNAPSHOT –spring.profiles.active=xxx
@Controller 处理HTTP请求
@RestController Spring4新增的注解,原来返回json,等同于@ResponseBody加上@Controller
@RequestMapping 配置URL映射
@Controller需要配合模板使用
使用Sring官方模板
<dependency> <groupId>org.springframe.boot</groupId> <artifactId>sprint-boot-starter-thymeleaf</artifactId> </dependency>
然后在resources/templates下创建模板文件
假设创建一个名为index.html的文件
然后在映射函数中return “index”即可
由于一般开发为前后端分离,所以不推荐使用模板,模板对性能损耗也大。
@RequestMapping多URL映射
使用集合
@RequestMapping(value = {"/hello", "/hi"}, method = RequestMethod.GET)
还可以给整个类设置URL
@RestController @RequestMapping("/hello") public class HelloController { @Autowired private GirlProperties girlProperties; @RequestMapping(value = "/say", method = RequestMethod.GET) public String say() { return girlProperties.getContent(); } }
最后访问:http://127.0.0.1:5000/hello/say
如果@RequestMapping不加method,则所有方法均可访问,多方式访问使用集合
@PathVariable 获取URL中的数据
@RequestParam 获取请求参数的值
@GetMapping 组合注解
@RequestMapping(value = "/say/{id}", method = {RequestMethod.POST, RequestMethod.GET}) public String say(@PathVariable("id") Integer id) { return "Hello "+ id; } http://127.0.0.1:5000/hello/say/1 这个方法不传id会显示404 @RequestMapping(value = "/say", method = {RequestMethod.POST, RequestMethod.GET}) public String say(@RequestParam("id") Integer id) { return "Hello "+ id; } http://127.0.0.1:5000/hello/say?id=1 //这个方法如果不传id会显示null @RequestMapping(value = "/say", method = {RequestMethod.POST, RequestMethod.GET}) public String say(@RequestParam(name = "id", required = false, defaultValue = "0") Integer id) { return "Hello "+ id; } //这个方法可以设置默认值(defaultValue),是否必须传这个值(required)
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
这些组合注解可以指定Mapping的方法
JPA(Java Persistence API)定义了一系列对象持久化的标准,目前实现这一规范的产品有Hibernate、TopLink等。
RESTful API设计
GET /girls 获取女生列表
POST /girls 创建一个女生
GET /girls/id 通过id查找一个女生
PUT /girls/id 通过id更新一个女生
DELETE /girls/id 通过id删除一个女生
添加依赖
<!--jpa--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!--mysql-connector--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
配置jpa和mysql
编写接口继承JpaRepository
public interface GirlRepository extends JpaRepository<Girl, Integer> { // 通过年龄查询 public List<Girl> findByAge(Integer age); }
进行CRUD
package com.yingjoy.demo.controller; import com.yingjoy.demo.Girl; import com.yingjoy.demo.GirlRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController public class GirlController { @Autowired private GirlRepository girlRepository; /** * 查询女生列表 */ @GetMapping(value = "/girls") public List<Girl> girlList() { return girlRepository.findAll(); } /** * 新增女生 */ @PostMapping(value = "/girls") public Girl girlAdd(@RequestParam("gradeNum") String gradeNum, @RequestParam("age") Integer age) { Girl girl = new Girl(); girl.setgradeNum(gradeNum); girl.setAge(age); return girlRepository.save(girl); } /** * 查询女生 */ @GetMapping(value = "/girls/{id}") public Girl girlFindOne(@PathVariable("id") Integer id) { return girlRepository.findById(id).get(); } /** * 更新女生 */ @PutMapping(value = "/girls/{id}") public Girl girlUpdate(@PathVariable("id") Integer id, @RequestParam("gradeNum") String gradeNum, @RequestParam("age") Integer age) { Girl girl = girlRepository.findById(id).get(); girl.setId(id); girl.setgradeNum(gradeNum); girl.setAge(age); return girlRepository.save(girl); } /** * 删除女生 */ @DeleteMapping(value = "/girls/{id}") public void girlDelete(@PathVariable("id") Integer id) { Girl girl = girlRepository.findById(id).get(); girlRepository.delete(girl); } /** * 按照年龄查询 */ @GetMapping("/girls/a/{age}") public List<Girl> girlFindByAge(@PathVariable("age") Integer age) { return girlRepository.findByAge(age); } }
注:put方法需要使用x-www-form-urlencoded传递数据,不能使用form-data
@Transactional 注释可以把方法变成事务