-
Notifications
You must be signed in to change notification settings - Fork 1
Example
κΈ°λ³Έμ μΈ μμ λ€μ μκ°ν©λλ€.
λ³Έ μμ λ REST APIμ λν΄ Fake Dataλ₯Ό 무λ£λ‘ μ 곡ν΄μ£Όλ JSONPlaceholderμ μ΄μ©νμ΅λλ€.
κ°μ₯ λ¨Όμ ν΄μΌν μΌμ HttpClient κ°μ²΄λ₯Ό μμ±νκ³ Service Interfaceλ₯Ό μ μνλ κ²μ
λλ€.
μμ μμ μ¬μ©ν base URLμ "http://jsonplaceholder.typicode.com"μ μ¬μ©νμ΅λλ€.
HttpClient httpClient = new HttpClient.Builder()
.baseUrl("http://jsonplaceholder.typicode.com")
.build();JSONPlaceholderμμ μ 곡νλ Post dataμ λν jsonμ λ€μκ³Ό κ°μ΅λλ€.
(μ΄ λ POSTλ HTTP Methodκ° μλ, κ²μκΈμ΄λ μλ―Έμ μμ΄ λ¨μ΄μ
λλ€.)
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}λ°λΌμ μ΄μ λ§κ² data classλ₯Ό μμ±ν©λλ€.
public class Post {
int userId;
int id;
String title;
String body;
public Post(int userId, int id, String title, String body) {
this.userId = userId;
this.id = id;
this.title = title;
this.body = body;
}
// getter and setter μλ΅
@Override
public String toString() {
return "Post{" +
"userId=" + userId +
", id=" + id +
", title='" + title + '\'' +
", body='" + body + '\'' +
'}';
}
}κ·Έλ¦¬κ³ μ΄λ₯Ό νμ©νμ¬ Service Interfaceλ₯Ό μ μν©λλ€.
public interface HttpService {
@RequestMapping(value="/posts", method=RequestMethod.GET)
CallTask<List<Post>> getPosts();
@RequestMapping(value="/posts", method=RequestMethod.POST)
CallTask<Post> postPosts(@RequestBody Post post);
}λ€μ λ¨κ³λ‘λ HttpClientμ create() λ©μλλ₯Ό ν΅ν΄ Service Interfaceμ μΈμ€ν΄μ€λ₯Ό μ£Όμ ν©λλ€.
HttpService httpService = httpClient.create(HttpService.class);μμ μ€λΉ μμ μ΄ λͺ¨λ μλ£λμλ€λ©΄ Http GET methodλ‘ Sync/Async ν΅μ μ μ€μ΅ν΄λ³΄κ² μ΅λλ€.
CallTask<List<Post>> posts = httpService.getPosts();
try {
Response<List<Post>> res = posts.execute(); // execute()λ₯Ό μ€ννμ λ μ€μ ν΅μ μ΄ μ΄λ£¨μ΄μ§λλ€.
List<Post> result = res.body();
for (Post post : result) {
System.out.println(post.toString());
}
} catch (IOException e) {
e.printStackTrace();
} CallTask<List<Post>> posts = httpService.getPosts();
posts.enqueue(new CallBack() {
@Override
public void onResponse(Response<?> response) throws IOException {
System.out.println(response.body());
}
@Override
public void onFailure(IOException e) {
System.out.println(e.getMessage());
}
});λ€μμ POST Http Methodλ₯Ό μ¬μ©ν μμ μ λλ€.
Post newPost = new Post(101, 101, "new Title", "new Body");
CallTask<Post> post = httpService.postPosts(newPost);
try {
Post result = post.execute().body();
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}μ΄μ λΆν°λ HttpService λ΄ λ©μλ μ μμ HttpClient κ°μ²΄ μμ± λ¨κ³λ§ μμ ν΄κ°λ©΄μ λ€μν μμ λ₯Ό μ€μ΅ν΄λ³΄κ² μ΅λλ€.
@PathParam μ μ¬μ©ν λλ Relative URLμ μ€κ΄νΈ{} λ΄ μΌμΉνλ name κ°μ΄ μμ΄μΌ ν©λλ€.
public interface HttpService {
@RequestMapping(value="/posts/{id}", method=RequestMethod.GET)
CallTask<Post> getPostsById(@PathParam("id") int id);
} CallTask<Post> post = httpService.getPostsById(5);
try {
Post result = post.execute().body();
System.out.println(result.toString());
} catch (IOException e) {
e.printStackTrace();
} @RequestMapping(value="/posts", method=RequestMethod.GET)
CallTask<List<Post>> getPostsByUserId(@Query("userId") Integer userId);@Queries λ₯Ό μ¬μ©νλ©΄ νλμ nameμ λν΄ μ¬λ¬ κ°μ valueλ₯Ό queryλ‘ κ΅¬μ±ν μ μμ΅λλ€.
@RequestMapping(value="/posts", method=RequestMethod.GET)
CallTask<List<Post>> getPostsById(@Queries("id") List<Integer> id);@QueryMap μ μ¬μ©νλ©΄ ν λ²μ λ€μν Query λ₯Ό ꡬμ±ν μ μμ΅λλ€.
@RequestMapping(value="/posts", method=RequestMethod.GET)
CallTask<List<Post>> getPostsByUserId(@QueryMap Map<String, Integer> userId);@Header, @HeaderMap λ₯Ό μ¬μ©νλ©΄ Requestμ Headerλ₯Ό μ€μ ν μ μμ΅λλ€.
@RequestMapping(value="/posts", method=RequestMethod.GET)
CallTask<List<Post>> getPostsWithHeader(@Header("content-type") String contentType); @RequestMapping(value="/posts", method=RequestMethod.GET)
CallTask<List<Post>> getPostsWithHeaderMap(@HeaderMap Map<String, String> headerMap);@Headers λ Parameter μμ€μ΄ μλ Method μμ€μ μ΄λ Έν μ΄μ μ λλ€.
@Headers({
"content-type:text/html",
"User-Agent:Naver"
})
@RequestMapping(value="/posts", method=RequestMethod.GET)
CallTask<List<Post>> getPostsWithHeaders();@FormUrlEncoded λ Request Bodyλ₯Ό ꡬμ±νλ νλμ λ°©μμΌλ‘, @Field μ @FieldMap μ μ¬μ©νμ¬ name:value νμμ κ°μ΅λλ€.
@RequestBody μ ν¨κ» μ¬μ©ν μ μμ΅λλ€.
@FormUrlEncoded
@RequestMapping(value="/posts", method=RequestMethod.POST)
CallTask<Post> postPostsFormUrlEncoded(@Field("userId") int userId, @Field("title") String title);@DynamicURL μ HttpClient κ°μ²΄μμ μ§μ ν base URLμ΄λ @RequestMapping μ relative URLκ³Ό μκ΄ μμ΄ λμ μΌλ‘ URLμ μ€μ ν μ μκ² ν΄μ£Όλ Method μμ€μ μ΄λ
Έν
μ΄μ
μ
λλ€.
@Query, @Queries, @QueryMap μ μ¬μ©ν μ μμΌλ @PathParam μ μ¬μ©ν μ μμ΅λλ€.
μ€μ URLμ μ
λ ₯ λ°λ κ²μ Parameter μμ€μ @URL μ΄λ
Έν
μ΄μ
μ΄ μ€μ λ Stringμ΄λ URL, URI λ±μ ν΅ν΄ κ°λ₯ν©λλ€.
@DynamicURL(method=RequestMethod.GET)
CallTask<List<Post>> getPostsByDynamicURL(@URL String url);