增删改查
1.在父工程pom.xml加入依赖
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.6</version> <scope>provided</scope> </dependency>
2.在模块eurekaclient中创建类com.example.entity.Student
package com.example.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class Student { private long id; private String name; private int age; }
在模块eurekaclient中创建类com.example.controller.StudentHandler
package com.example.controller; import com.example.entity.Student; import com.example.repository.StudentRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.Collection; @RestController @RequestMapping("/student") public class StudentHandler { @Autowired private StudentRepository studentRepository; @GetMapping("/findAll") public Collection<Student> findAll(){ return studentRepository.findAll(); } @GetMapping("/findById/{id}") public Student findById(@PathVariable("id") long id){ return studentRepository.findById(id); } @PostMapping("/save") public void save(@RequestBody Student student) { studentRepository.saveOrUpdate(student); } @PutMapping("/update") public void update(@RequestBody Student student) { studentRepository.saveOrUpdate(student); } @DeleteMapping("/deleteById/{id}") public void deleteById(@PathVariable("id") long id) { studentRepository.deleteById(id); } }
3.重启模块eurekaclient
浏览器或postman中验证http://localhost:8010/student/findAll
http://localhost:8010/student/findById/1
post: http://localhost:8010/student/save
Body { "id": 6,"name": "王麻子","age": 22}
RestTemplate的使用
什么是RestTemplate?
RestTemplate是Spring 框架提供的基于REST的服务组件,底层是对HTTP请求及响应进行了封装,提供了很多访问REST服务的方法,可简化代码开发。
如何使用RestTemplate?
1.创建Module,名字 resttemplate
2.创建实体类com.example.entity.Student
package com.example.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class Student { private long id; private String name; private int age; }
3.创建RestHandler类
com.example.controller.RestHandler
package com.example.controller; import com.example.entity.Student; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.client.RestTemplate; import java.util.Collection; @RestController @RequestMapping("/rest") public class RestHandler { @Autowired private RestTemplate restTemplate; @GetMapping("/findAll") public Collection<Student> findAll(){ return restTemplate.getForEntity("http://localhost:8010/student/findAll",Collection.class).getBody(); } @GetMapping("/findById/{id}") public Student findById(@PathVariable("id") long id){ return restTemplate.getForEntity("http://localhost:8010/student/findById/{id}",Student.class,id).getBody(); } @PostMapping("/save") public void save(@RequestBody Student student) { restTemplate.postForEntity("http://localhost:8010/student/save",student,null).getBody(); } @PutMapping("/update") public void update(@RequestBody Student student) { restTemplate.put("http://localhost:8010/student/update",student); } @DeleteMapping("/deleteById/{id}") public void deleteById(@PathVariable("id") long id) { restTemplate.delete("http://localhost:8010/student/delete/{id}",id); } }
4.创建启动类RestTemplateApplication
com.example.RestTemplateApplication
package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication public class RestTemplateApplication { public static void main(String[] args) { SpringApplication.run(RestTemplateApplication.class); } @Bean public RestTemplate restTemplate(){ return new RestTemplate(); } }
5.启动模块resttemplate
浏览器或postman中验证http://localhost:8080/rest/findAll