SpringMVCController-Spring stage study notes 01

The content of the course comes from the geek time playing Spring Family Bucket, invaded and deleted , the link is as follows
https://time.geekbang.org/course/intro/100023501

Chapter Six

SpringMVCController

Know SpringMVC

The core of SpringMVC is DispatcherServlet, which is an entry point for all requests

The core component
Controller (request) processing logic in
DispatcherServlet XXXResolver resolver
HandlerMapping request and Controller mapping

Common @ annotation

@RequestBody Requested message body
@ResponseBody Response message body
@ResponseStatus Response code

code

CoffeeController class

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();
    }
}

You can see Mapping annotations such as RequestMapping, which can be written on the class declaration or on the method declaration; on the class, it means that the entire Controller class needs to be added to the Mapping request, which is equivalent to the prefix
and the declaration on the method. Only the request of this method,
specifically, the getAll() method requires the request of /coffee and / to be mapped to this method

There are two annotations @Controller and @ResponseBody in this class, which can be written as the annotation @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);
    }
}

You can see the logic of the program. Send a post request to the /order/ combination request. The request content is the String and List defined in NewOrderRequest. The specified status code is HttpStatus.CREATED, and the system is 201

NewOrderRequest class

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;
}

Recommended tools

Postman
https://www.postman.com/downloads/
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43596589/article/details/112857335