SpringMVCController-Spring阶段学习笔记01

学习笔记@玩转Spring全家桶

课程内容来自极客时间玩转Spring全家桶,侵私删,链接如下
https://time.geekbang.org/course/intro/100023501

第六章

SpringMVCController

认识SpringMVC

SpringMVC的核心是DispatcherServlet,是所有请求的一个入口

DispatcherServlet中的核心组件
Controller (请求)处理逻辑
XXXResolver 解析器
HandlerMapping 请求与Controller的映射

常用@注解

@RequestBody 请求的报文体
@ResponseBody 响应的报文体
@ResponseStatus 响应码

程序代码

CoffeeController类

package geektime.spring.springbucks.waiter.controller;

import geektime.spring.springbucks.waiter.model.Coffee;
import geektime.spring.springbucks.waiter.service.CoffeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/coffee")
public class CoffeeController {
    @Autowired
    private CoffeeService coffeeService;

    @GetMapping("/")
    @ResponseBody
    public List<Coffee> getAll() {
        return coffeeService.getAllCoffee();
    }
}

可以看到RequestMapping等Mapping的注解,既可以写在类声明上,也可以写在方法声明上;在类上,表示整个Controller类都需要加上Mapping上请求,相当于prefix
而声明在方法上则只此方法的请求
具体来说就是getAll()方法需要/coffee和/这两部分的请求,才会映射到这个方法

此类中存在@Controller与@ResponseBody两个注解,可以写成@RestController这一个注解

CoffeeOrderController类

package geektime.spring.springbucks.waiter.controller;

import geektime.spring.springbucks.waiter.controller.request.NewOrderRequest;
import geektime.spring.springbucks.waiter.model.Coffee;
import geektime.spring.springbucks.waiter.model.CoffeeOrder;
import geektime.spring.springbucks.waiter.service.CoffeeOrderService;
import geektime.spring.springbucks.waiter.service.CoffeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
@Slf4j
public class CoffeeOrderController {
    @Autowired
    private CoffeeOrderService orderService;
    @Autowired
    private CoffeeService coffeeService;

    @PostMapping("/")
    @ResponseStatus(HttpStatus.CREATED)
    public CoffeeOrder create(@RequestBody NewOrderRequest newOrder) {
        log.info("Receive new Order {}", newOrder);
        Coffee[] coffeeList = coffeeService.getCoffeeByName(newOrder.getItems())
                .toArray(new Coffee[] {});
        return orderService.createOrder(newOrder.getCustomer(), coffeeList);
    }
}

可以看到程序的逻辑,向/order/这组合请求下发送post请求,请求内容是在NewOrderRequest中定义的String与List,指定状态码是HttpStatus.CREATED,系统中为201

NewOrderRequest 类

package geektime.spring.springbucks.waiter.controller.request;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.List;

@Getter
@Setter
@ToString
public class NewOrderRequest {
    private String customer;
    private List<String> items;
}

推荐工具

Postman
https://www.postman.com/downloads/
SpringMVCController-Spring阶段学习笔记01

上一篇:CF #405 (Div. 2) B. Bear ad Friendship Condition (dfs+完全图)


下一篇:利用Proxy.newProxyInstance实现AOP