Sparrow HTTP Client
An modular, easy-to-use HTTP client inspired by Openfeign/feign.
Quick Start
Spring Boot Starter
- Add Spring Boot Starter dependency
<dependencies>
<dependency>
<groupId>win.minaandyyh.sparrow</groupId>
<artifactId>sparrow-spring-boot-starter</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
- Add
@EnableSparrow
annotation on main class
@SpringBootApplication
@EnableSparrow
public class AwesomeProjectApplication {
public static void main(String[] args) {
SpringApplication.run(AwesomeProjectApplication.class, args);
}
}
- Define data model
@Data // Lombok
public class Post {
private Integer userId;
private Integer id;
private String title;
private String body;
}
- Define your client interface
@SparrowClient(url = "https://jsonplaceholder.typicode.com")
public interface PostAPI {
@Get(path = "/posts", produces = MediaType.APPLICATION_JSON)
List<Post> all();
@Get(path = "/post/{id}", produces = MediaType.APPLICATION_JSON)
Post byId(@UrlVariable(name = "id") Integer id);
}
- Use this interface just like other Spring beans
@Service
public class SomeService {
private final PostAPI postApi;
@Autowired
public SomeService(PostAPI postApi) {
this.postApi = postApi;
}
// ...
// Currently no java.util.Optional support, but it will be added later
// Just for demonstration so no null-check present
public Post getPost(Integer id) {
return postApi.byId(id);
}
}
Non-Spring project
- Add dependencies
<dependencies>
<dependency>
<groupId>win.minaandyyh</groupId>
<artifactId>sparrow-core</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>win.minaandyyh</groupId>
<artifactId>sparrow-jackson-codec</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
- Define data model
@Data // Lombok
public class Post {
private Integer userId;
private Integer id;
private String title;
private String body;
}
- Define your client interface
@SparrowClient(url = "https://jsonplaceholder.typicode.com")
public interface PostAPI {
@Get(path = "/posts", produces = MediaType.APPLICATION_JSON)
List<Post> all();
@Get(path = "/post/{id}", produces = MediaType.APPLICATION_JSON)
Post byId(@UrlVariable(name = "id") Integer id);
}
- Build client using
Sparrow
builder and use it somewhere
public class SomeAwesomeClass {
// ...
private final PostAPI postApi = Sparrow.<PostAPI>newClient()
.encoder(new JacksonJsonEncoder())
.decoder(new JacksonJsonDecoder())
.client(PostAPI.class)
.handler(JDKHttpClientRequestHandler.defaultHandler())
.build();
public Post getPost(Integer id) {
return postApi.byId(id);
}
}
License
This project is licensed under the Apache License, Version 2.0.
Roadmap
Currently WIP
- Add
java.util.Optional
support for better null-safe capability. - Refactor request object process chain for better usage.
What’s up next
- Add logging and error handling capabilities.
- Comply with IETF standards.
- Integrate with Spring Cloud framework to enable microservices RPC capabilities.
- Integrate with Spring Data for better paging capabilities.