Skip to content

Commit fbfd73f

Browse files
authored
1주차 스터디
- 네이버 오픈 API 를 연동한다.
1 parent 2d4b063 commit fbfd73f

File tree

5 files changed

+99
-2
lines changed

5 files changed

+99
-2
lines changed

build.gradle

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,25 @@ group = 'com.example'
88
version = '0.0.1-SNAPSHOT'
99
sourceCompatibility = '1.8'
1010

11+
configurations {
12+
compileOnly {
13+
extendsFrom annotationProcessor
14+
}
15+
}
16+
1117
repositories {
1218
mavenCentral()
1319
}
1420

1521
dependencies {
1622
implementation 'org.springframework.boot:spring-boot-starter-web'
23+
compileOnly 'org.projectlombok:lombok'
24+
annotationProcessor 'org.projectlombok:lombok'
1725
testImplementation('org.springframework.boot:spring-boot-starter-test') {
1826
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
1927
}
2028
}
2129

2230
test {
2331
useJUnitPlatform()
24-
}
32+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.example.demo;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.http.HttpEntity;
5+
import org.springframework.http.HttpHeaders;
6+
import org.springframework.http.HttpMethod;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.stereotype.Service;
9+
import org.springframework.web.client.RestTemplate;
10+
11+
@Service
12+
public class BlogService {
13+
14+
@Value("${naver.openapi.blogUrl}")
15+
private String naverOpenApiUrl;
16+
17+
@Value("${naver.openapi.clientId}")
18+
private String naverOpenApiClientId;
19+
20+
@Value("${naver.openapi.clientSecret}")
21+
private String naverOpenApiClientSecret;
22+
23+
public ResponseEntity<ResponseNaverBlog> findByQuery(String query){
24+
25+
26+
//TODO: 코드 리팩토링
27+
28+
RestTemplate restTemplate = new RestTemplate();
29+
30+
HttpHeaders httpHeaders = new HttpHeaders();
31+
httpHeaders.add("X-Naver-Client-Id", naverOpenApiClientId);
32+
httpHeaders.add("X-Naver-Client-Secret", naverOpenApiClientSecret);
33+
34+
String url = naverOpenApiUrl + "?query=" + query;
35+
36+
return restTemplate.exchange(url, HttpMethod.GET, new HttpEntity(httpHeaders), ResponseNaverBlog.class);
37+
38+
}
39+
40+
41+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.example.demo;
2+
3+
import org.springframework.http.ResponseEntity;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@RestController
9+
@RequestMapping("/api")
10+
public class HomeController {
11+
12+
private final BlogService blogService;
13+
14+
public HomeController(BlogService blogService) {
15+
this.blogService = blogService;
16+
}
17+
18+
@GetMapping("/blog")
19+
public ResponseEntity<ResponseNaverBlog> getTest(){
20+
21+
return blogService.findByQuery("스프링부트");
22+
}
23+
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.example.demo;
2+
3+
import lombok.Data;
4+
5+
import java.io.Serializable;
6+
import java.util.List;
7+
8+
@Data
9+
public class ResponseNaverBlog implements Serializable {
10+
11+
private Integer total;
12+
private List<naverDocument> items;
13+
14+
@Data
15+
public static class naverDocument{
16+
String title;
17+
String link;
18+
String bloggername;
19+
String description;
20+
String postdate;
21+
}
22+
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
1+
naver.openapi.blogUrl=https://openapi.naver.com/v1/search/blog.json
2+
naver.openapi.clientId=-
3+
naver.openapi.clientSecret=-

0 commit comments

Comments
 (0)