Skip to content

Instantly share code, notes, and snippets.

@LenarBad
Last active February 27, 2018 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LenarBad/152e8ed736e407e64238b5d46939c95b to your computer and use it in GitHub Desktop.
Save LenarBad/152e8ed736e407e64238b5d46939c95b to your computer and use it in GitHub Desktop.
REST Service Client base on created HttpClient. Example for BookService
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