[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);
    }
}
原文地址:https://www.cnblogs.com/Answer1215/p/14055259.html