(Spring Boot)Controller実装

package webapp.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import webapp.dao.WebAppDao;
import webapp.entity.Author;
import webapp.entity.Book;
import webapp.response.AuthorResponse;

@Controller
@RequestMapping(value = "/webapp")
public class WebAppController {
    
    @Autowired
    private WebAppDao dao;

    @RequestMapping(value = "/author", method = RequestMethod.GET)
    @ResponseBody
    public AuthorResponse author(@RequestParam String id) {
        
        int authorId = Integer.parseInt(id);
        
        // Author
        List<Author> authorList = dao.selectAuthor(authorId);
        
        // Books
        List<Book> bookList = dao.selectAuthorBooks(authorId);
        
        // Response
        AuthorResponse response = new AuthorResponse(
                authorList.get(0).getId(), 
                authorList.get(0).getName(), 
                authorList.get(0).getBirthDay(), 
                bookList);
        
        return response;
    }
}

アクセスは、 http://localhost:8080/webapp/author?id=2 です。

これで一連の Spring Boot & Doma2 を終了とする。


(Spring Boot)Spring Tools Suite(STS)のセットアップ

(Spring Boot)Doma2を使う準備

(Spring Boot)エンティティの準備

(Spring Boot)Dao実装

(Spring Boot)Responseオブジェクト

(Spring Boot)Controller実装