com.mycompany.springboot.controller.IndexRestController.java Source code

Java tutorial

Introduction

Here is the source code for com.mycompany.springboot.controller.IndexRestController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.springboot.controller;

import com.mycompany.springboot.model.Comment;
import com.mycompany.springboot.service.CommentService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author josiamu
 */
@RestController
@RequestMapping("/api")
public class IndexRestController {

    @Autowired
    private CommentService commentService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    private List<Comment> get() {
        return commentService.getComment();
    }

    @RequestMapping(value = "/", method = { RequestMethod.POST, RequestMethod.DELETE })
    private List<Comment> post(@Validated @RequestBody Comment comment) {
        return commentService.saveComment(comment);
    }
}