zoukankan      html  css  js  c++  java
  • [Java Srping] @RestController and @Controller

    We can use @Controller to return a view with data:

    package com.frankmoley.lil.learningspring.web;
    
    import com.frankmoley.lil.learningspring.busniess.domain.RoomReservation;
    import com.frankmoley.lil.learningspring.busniess.service.ReservationService;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    
    import java.util.Date;
    import java.util.List;
    
    @Controller
    @RequestMapping("/reservations")
    public class RoomReservationWebController {
    
        @Autowired
        private ReservationService reservationService;
    
        @GetMapping
        public String getReservations (@RequestParam(value="date", required = false) String dateString, Model model) {
            Date date = DateUtils.createDateFromDateString(dateString);
            List<RoomReservation> roomReservations = this.reservationService.getRoomReservationsForDate(date);
            model.addAttribute("roomReservations", roomReservations);
            return "reservations";
        }
    }

    We can also return JSON data:

    package com.frankmoley.lil.learningspring.web;
    
    import com.frankmoley.lil.learningspring.busniess.domain.RoomReservation;
    import com.frankmoley.lil.learningspring.busniess.service.ReservationService;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.Date;
    import java.util.List;
    
    @RestController
    @RequestMapping("api/reservations")
    public class RoomResercationWebServiceController {
        private final ReservationService reservationService;
    
        @Autowired
        public RoomResercationWebServiceController(final ReservationService reservationService) {
            this.reservationService = reservationService;
        }
    
        @GetMapping
        public List<RoomReservation> getRoomReservations(@RequestParam(name="date", required = false)String dateString) {
            Date date = DateUtils.createDateFromDateString(dateString);
            return this.reservationService.getRoomReservationsForDate(date);
        }
    }
  • 相关阅读:
    linux-who
    Linux开机禁用开启防火墙
    linux环境vnc安装
    NFS配置及开机自动挂载
    yum list失败
    镜像文件挂载及本地yum搭建
    weblogic在64位windows的设置
    linux操作系统语言更改
    Linux磁盘空间扩容(LVM)
    Nginx 拒接服务漏洞(CVE-2016-0747)整改
  • 原文地址:https://www.cnblogs.com/Answer1215/p/14055259.html
Copyright © 2011-2022 走看看