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);
        }
    }
  • 相关阅读:
    字符匹配算法之KMP
    rabbitmq_hearbeat
    rabbitmq_config
    postgres SQL编译过程
    postgres启动过程分析
    postgres源码目录结构
    Js两种post方式(转)
    PHP-MySQL,PHP-MySQLi,PDO的差异
    CSS属性中Display与Visibility的不同
    PHP中include路径修改
  • 原文地址:https://www.cnblogs.com/Answer1215/p/14055259.html
Copyright © 2011-2022 走看看