Spring 搭建REST 后端

2019-04-06  本文已影响0人  柳源居士

Rest的英文全称是(Representational status transfer),可以理解为表述性状态转移。
传统的Spring MVC通过控制器,返回一个ModelAndView对象,包含了模型对象以及视图,sping mvc再通过ViewResolver,来解析视图并渲染数据。
而Rest主要是针对远程调用的,如移动App的后端,或者前后端分离的应用结构,它的控制器不怎么关心视图,只关心数据类型。

Rest与传统控制器代码结构极其相似,不同之处在于注解的不同。
当我们声明一个mvc的控制器时:

@Controller
@RequestMapping("/customer")
public class CustomerController {
    
    private CustomerRepository customerRepository;

    @Autowired
    public CustomerController(CustomerRepository customerRepository) {
        this.customerRepository = customerRepository;
    }

    @RequestMapping(value="/allCustomer",method = RequestMethod.GET,
                    produces="application/json;charset=UTF-8")
    public List<Customer>  customerList(){
        List<Customer> customerList=customerRepository.findAll();
        if(customerList!=null){
            return customerList;
        }
        return new ArrayList<Customer>();
    }

    @RequestMapping(value = "/{name}",method = RequestMethod.GET)
    public List<Customer> customerByName(@PathVariable("name") String name){
        List<Customer> customers=customerRepository.findCustomerByName(name);
        return  customers;
    }

    @RequestMapping(value="/addNew",method = RequestMethod.POST,
    consumes = "application/json;charset=UTF-8")
    public Customer addNewCustomer(@RequestBody Customer customer){
        return customerRepository.save(customer);
        
    }
    
}

普通的Controller返回ModelAndView。
通过改变注解,可以将它变成Rest的控制器。

@Controller
@RequestMapping("/customer")
public class CustomerController {
   @RequestMapping(value="/allCustomer",method = RequestMethod.GET,
    produces="application/json;charset=UTF-8")
    public @ResponseBody List<Customer>  customerList(){
        List<Customer> customerList=customerRepository.findAll();
        if(customerList!=null){
            return customerList;
        }
        return new ArrayList<Customer>();
    }
}

通过@ResponseBody ,我们将返回的对象作为资源发给客户端。

Spring 通过进一步简化注解结构,使用@RestController来完成控制器声明, @RestController本身涵盖了@ResponseBody的含义。这样,控制器的代码就更加简单了。

@RestController
@RequestMapping("/customer")
public class CustomerController {
    
    private CustomerRepository customerRepository;

    @Autowired
    public CustomerController(CustomerRepository customerRepository) {
        this.customerRepository = customerRepository;
    }

    @RequestMapping(value="/allCustomer",method = RequestMethod.GET,
    produces="application/json;charset=UTF-8")
    public List<Customer>  customerList(){
        List<Customer> customerList=customerRepository.findAll();
        if(customerList!=null){
            return customerList;
        }
        return new ArrayList<Customer>();
    }

    @RequestMapping(value = "/{name}",method = RequestMethod.GET)
    public List<Customer> customerByName(@PathVariable("name") String name){
        List<Customer> customers=customerRepository.findCustomerByName(name);
        return  customers;
    }

    @RequestMapping(value="/addNew",method = RequestMethod.POST,
    consumes = "application/json;charset=UTF-8")
    public Customer addNewCustomer(@RequestBody Customer customer){
        return customerRepository.save(customer);
        
    }
    
}
上一篇下一篇

猜你喜欢

热点阅读