springboot 讲解RestController中获取请求的各种参数

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/tvseries")
public class TvSeriesController {
    private static final Log log = LogFactory.getLog(TvSeriesController.class);

    @GetMapping
    public List getAll() {
        if (log.isTraceEnabled()){
            log.trace("getAll();被调用了");
        }
        List list = new ArrayList<>();
        list.add(createPoi());
        list.add(createWestWorld());
        return list; //返回一个电视剧列表
    }

    @GetMapping("/{id}")
    public TvseriesDto getOne(@PathVariable int id){
        if (log.isTraceEnabled()){
            log.trace("getOne " + id);
        }
        if (id==101) {
            return createWestWorld();
        }else if (id ==102){
            return createPoi();
        }else{
            throw new ResourceNotFoundException();
        }
    }

    private TvseriesDto createPoi(){
        Calendar c = Calendar.getInstance();
        c.set(2016,Calendar.SEPTEMBER,2,0,0);
        return new TvseriesDto(1,"Person of Interest",5,c.getTime());
    }

    private TvseriesDto createWestWorld(){
        Calendar c = Calendar.getInstance();
        c.set(2016,Calendar.OCTOBER,2,0,0);
        return new TvseriesDto(1,"WestWorld",1,c.getTime());
    }
}

springboot 讲解RestController中获取请求的各种参数_第1张图片

springboot 讲解RestController中获取请求的各种参数_第2张图片

springboot 讲解RestController中获取请求的各种参数_第3张图片

你可能感兴趣的:(Spring,Boot)