to {GET [/api/board]}: There is already 'boardApiController' bean method
com.toy.board.web.BoardApiController#all() mapped.

 

Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'boardApiController' method 

 

 

RESTful API 만들다가 생긴 오류

package com.toy.board.web;

import java.util.List;

import com.toy.board.domain.Board;
import com.toy.board.reopsitory.BoardRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
class BoardApiController {


    @Autowired
    private BoardRepository boardRepository;



    @GetMapping("/board")
    List<Board> all() {
        return boardRepository.findAll();
    }


    @PostMapping("/board")
    Board newBoard(@RequestBody Board newBoard) {
        return boardRepository.save(newBoard);
    }

    // Single item

    @GetMapping("/board/{id}")
    Board one(@PathVariable Long id) {

        return boardRepository.findById(id).orElse(null);
    }

    @GetMapping("/board")
    List<Board> findByTitle(@RequestParam(required = false) String title) {
        if (StringUtils.isEmpty(title)) {
            return boardRepository.findAll();
        } else {
            return boardRepository.findByTitle(title);
        }
    }


    @PutMapping("/board/{id}")
    Board replaceBoard(@RequestBody Board newBoard, @PathVariable Long id) {

        return boardRepository.findById(id)
                .map(Board -> {
                    Board.setTitle(newBoard.getTitle());
                    Board.setContent(newBoard.getContent());
                    return boardRepository.save(Board);
                })
                .orElseGet(() -> {
                    newBoard.setId(id);
                    return boardRepository.save(newBoard);
                });
    }

    @DeleteMapping("/board/{id}")
    void deleteBoard(@PathVariable Long id) {
        boardRepository.deleteById(id);
    }
}

@GetMapping 에 같은 이름이나 주소를 설정했을 때 생기는 오류이다.

쉽게 해결

 

 

'Spring' 카테고리의 다른 글

Spring - Application Context  (0) 2022.09.26
Spring - pom.xml  (0) 2022.09.14
Spring - web.xml  (0) 2022.09.13
Spring - Maven  (0) 2022.09.13
Spring - java.validation  (0) 2022.09.05
복사했습니다!