iT邦幫忙

2023 iThome 鐵人賽

DAY 26
0
Software Development

我在 Spring Boot 3 裡面挖呀挖呀挖系列 第 26

Day25 - Functional Endpoints

  • 分享至 

  • xImage
  •  

Day25 - Functional Endpoints

前言

Spring MVC 5.2起提供一種輕量級functional programing model的方式來處理Web請求,就讓我們來看看有別於Controller+RequestMapping的方式吧

專案建立

https://ithelp.ithome.com.tw/upload/images/20231011/201280848gaDe7Wxa2.png
https://ithelp.ithome.com.tw/upload/images/20231011/201280848tus8HMJM3.png

Functional Endpoints

Web請求處理方式

  1. @Controller + @RequestMapping:耦合式(路由與業務耦合)
@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello(){
        return "Hello";
    }
}
  1. Functional Endpoints:分離式(路由與業務分離)
Core
  1. RouterFunction:定義路由訊息、發什麼請求、由誰處理
  2. RequestPredicate:定義請求:請求方式、請求參數
  3. ServerRequest:封裝請求完整參數
  4. ServerResponse:封裝回應完整參數

demo code

Bean

@Data
@AllArgsConstructor
public class Person {
    private String name;
    private String id;
}

Repository

@Slf4j
@Repository
public class PersonRepository {

    public void create(Person person){
        log.info("===Repository=== create person:"+person);
    }
    public Person findById(String id){
        log.info("===Repository=== getPersonById:"+id);
        return new Person("james","3");
    }
    public List<Person> findAll(){
        log.info("===Repository=== findAll");

        List<Person> list = new ArrayList<>();
        Person p1 = new Person("p1","1");
        Person p2 = new Person("p2","2");
        list.add(p1);
        list.add(p2);
        return list;
    }
    public void update(Person person){
        log.info("===Repository=== update;"+ person);
    }
    public void delete(String id){
        log.info("===Repository=== delete;"+id);
    }

}

Handler

@Component
public class PersonHandler {
//    @Autowired
    private PersonRepository personRepository;

    public PersonHandler(PersonRepository personRepository) {
        this.personRepository = personRepository;
    }


    public ServerResponse getPersonById(ServerRequest request) {
        Person person = personRepository.findById(request.pathVariable("id"));
        return ServerResponse.ok().body(person);
    }
    public ServerResponse updatePerson(ServerRequest request) throws ServletException, IOException {
        Person person = request.body(Person.class);
        personRepository.update(person);
        return ServerResponse.ok().build();
    }

    public ServerResponse listPeople(ServerRequest request) {
        List<Person> list = personRepository.findAll();
        return ServerResponse.ok().body(list);
    }

    public ServerResponse createPerson(ServerRequest request) throws ServletException, IOException {
        //提取请求体
        Person body = request.body(Person.class);
        personRepository.create(body);
        return ServerResponse.ok().build();
    }

    public ServerResponse deletePerson(ServerRequest request) throws ServletException, IOException {
        personRepository.delete(request.pathVariable("id"));
        return ServerResponse.ok().build();
    }

}

PersonRouterConfig,可以比較一下先前使用Controller + RequestMapping的方式

@Configuration
public class PersonRouterConfig {

    @Bean
    public RouterFunction<ServerResponse> routes(PersonHandler handler) {
        return RouterFunctions.route(GET("/api/person"), handler::listPeople)
                .andRoute(POST("/api/person"), handler::createPerson)
                .andRoute(GET("/api/person/{id}"), handler::getPersonById)
                .andRoute(PUT("/api/person/{id}"), handler::updatePerson)
                .andRoute(DELETE("/api/person/{id}"), handler::deletePerson);
    }
}

demo result

GET getPersonById
https://ithelp.ithome.com.tw/upload/images/20231011/20128084lF6PjhlnJg.png
GET listPeople
https://ithelp.ithome.com.tw/upload/images/20231011/20128084ly5NaY89em.png
POST createPerson
https://ithelp.ithome.com.tw/upload/images/20231011/20128084qF9YD1Vq1d.png
https://ithelp.ithome.com.tw/upload/images/20231011/20128084Wu68bC3fs0.png
DELETE deletePerson
https://ithelp.ithome.com.tw/upload/images/20231011/20128084D1H8l8K9zs.png
https://ithelp.ithome.com.tw/upload/images/20231011/201280844oJLbS7hiQ.png

Reference


上一篇
Day24 - Problem Details
下一篇
Day26 - SSM
系列文
我在 Spring Boot 3 裡面挖呀挖呀挖31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言