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);
        }
    }
  • 相关阅读:
    C#制作在线升级程序
    C#中使用GUID的笔记
    C# WinForm 上传图片,文件到服务器的方法Uploader.ashx
    Java学习---多线程的学习
    Java学习---异常处理的学习
    Java学习---TCP Socket的学习
    Java学习---IO操作
    Java学习---JAVA的类设计
    Java学习---InetAddress类的学习
    Java学习---面向对象的远程方法调用[RMI]
  • 原文地址:https://www.cnblogs.com/Answer1215/p/14055259.html
Copyright © 2011-2022 走看看