2017-03-06 5 views
1

私は自動休止エンドポイントとHATEOASのためにSpring Data Restを使用しています。私はローカルホストに行くとき:8080私が取得:Springデータレストを使用するコントローラとリポジトリの同じURLマッピング

{ 
    "_links": { 
    "books": { 
     "href": "http://localhost:8080/books{?page,size,sort}", 
     "templated": true 
    }, 
    "users": { 
     "href": "http://localhost:8080/users" 
    }, 
    "customers": { 
     "href": "http://localhost:8080/customers" 
    }, 
    "profile": { 
     "href": "http://localhost:8080/profile" 
    } 
    } 
} 

GETする@ localhostの:8080 /本を私に与える:ここで There was an unexpected error (type=Method Not Allowed, status=405). Request method 'GET' not supported

は私のレポです:

public interface BookRepository extends PagingAndSortingRepository<Book, Long> { 
    Optional<Book> findByIsbn(String isbn); 
} 

マイコントローラ:

@RestController 
public class BookController { 
    private final BookService bookService; 

    @Autowired 
    public BookController(BookService bookService) { 
    this.bookService = bookService; 
    } 

    @PostMapping("/books") 
    @ResponseStatus(HttpStatus.CREATED) 
    public Book newToDo(@RequestBody BookDTO bookDTO) { 
    return bookService.addNewBook(bookDTO); 
    } 

    @PutMapping("/books/{id}") 
    public ResponseEntity<?> editToDo(@PathVariable("id") Long id, @RequestBody double newPrice) { 
    return new ResponseEntity<>(bookService.changePrice(id, newPrice), HttpStatus.OK); 
    } 
} 

コントローラなしGET @ localhost:8080/books作品完全に細かい - リポジトリ自体がこのエンドポイントを設定し、私はすべての私の書籍を検索することができます。これらのPOSTメソッドとPUTメソッドを追加すると、エラーが発生します。 GET requests @ localhost:8080/booksのリポジトリとPOST @ localhost:8080/booksの通常のコントローラメソッドを使用する方法はありますか?カスタムエンドポイントの実装のために

答えて

0

@RestControllerSpring Data RESTとしてはSpring Web@RestController注釈は異なる流れは、単に仕事や紛争Spring Data RESTないかもしれません使用している場合、代わりに@RepositoryRestControllerを使用するようにしてください。

また、HATEOASリンクをサポートするために@ExposesResourceForアノテーションを追加することもできます。

関連する問題