springboot REST風格接口

 


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yuyi.dao.PersonDAO;
import com.yuyi.model.PersonDTO;

/**
 * rest風格
 */
@RestController
@RequestMapping("person")
public class RESTController {

    @Autowired
    private PersonDAO personDAO;
    
    /**
     * 查詢 根據Id
     */
    @GetMapping("{id}")
//    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public PersonDTO getPersonById(@PathVariable Integer id){
         return personDAO.getPersonById(id);
    }
    
    /**
     * 查詢全部
     */
    @GetMapping
//    @RequestMapping(value = "", method = RequestMethod.GET)
    public List<PersonDTO> listPerson(){
        return personDAO.listPerson();
    }
    
    /**
     * 添加數據
     */
    @PutMapping
//    @RequestMapping(value = "", method = RequestMethod.PUT)
    public Integer insertPerson(
            @RequestParam String pname,
            @RequestParam String age
            ){
        PersonDTO person = new PersonDTO(pname, age);
        return personDAO.insertPerson(person);
    }
    
    /**
     * 修改數據
     */
    @PatchMapping
//    @RequestMapping(value = "", method = RequestMethod.PATCH)
    public Integer updatePerson(PersonDTO person){
        return personDAO.updatePerson(person);
    }
    
    /**
     * 刪除數據
     */
    @DeleteMapping("{id}")
//    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    Integer deletePerson(@PathVariable Integer id){
        return personDAO.deletePerson(id);
    }
    

}

(圖片引自《基於springboot創建RESTful風格接口》:https://www.jianshu.com/p/733d788ea94d) 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章