Last active
February 27, 2018 21:04
-
-
Save LenarBad/152e8ed736e407e64238b5d46939c95b to your computer and use it in GitHub Desktop.
REST Service Client base on created HttpClient. Example for BookService
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import io.lenar.examples.spring.clients.response.BookResponse; | |
import io.lenar.examples.spring.clients.response.BooksResponse; | |
import io.lenar.examples.spring.clients.response.Response; | |
import io.lenar.examples.spring.http.HttpClient; | |
import io.lenar.examples.spring.model.Book; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.stereotype.Component; | |
@Component | |
public class BookServiceClient { | |
@Value("${bookService.url}") | |
private String serviceUrl; | |
@Autowired | |
HttpClient http; | |
public BooksResponse findBooks(String author) throws IOException { | |
String url = author == null ? serviceUrl : serviceUrl + "?author=" + author; | |
return new BooksResponse(http.get(url)); | |
} | |
public BooksResponse findBooks() throws IOException { | |
return new BooksResponse(http.get(serviceUrl)); | |
} | |
public BookResponse findBook(String id) throws IOException { | |
return new BookResponse(http.get(serviceUrl + "/" + id)); | |
} | |
public BookResponse createBook(Book book) throws IOException { | |
return new BookResponse(http.post(serviceUrl, book)); | |
} | |
public BookResponse updateBook(String id, Book book) throws IOException { | |
return new BookResponse(http.put(serviceUrl + "/" + id, book)); | |
} | |
public Response deleteBook(String id) throws IOException { | |
return new Response(http.delete(serviceUrl + "/" + id)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See also
REST API client for TestNG and Spring Boot using Apache Http Components. Part 2
Response classes for Service Client gist
HttpClient gist