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);
        }
    }
  • 相关阅读:
    openJudge计算概论-谁考了第k名
    OpenJudge计算概论-求平均年龄
    OpenJudge计算概论-能被3,5,7整除的数
    OpenJudge计算概论-计算书费
    OpenJudge计算概论-计算三角形面积【海伦公式】
    OpenWrt 中安装配置Transmission
    OpenWrt中wifidog的配置及各节点页面参数
    Linux中后台执行任务
    通过ionice和nice降低shell脚本运行的优先级
    OpenWrt中对USB文件系统的操作, 以及读写性能测试
  • 原文地址:https://www.cnblogs.com/Answer1215/p/14055259.html
Copyright © 2011-2022 走看看