diff --git a/src/main/java/com/ajou/procoding/myweb/controller/MyWebController.java b/src/main/java/com/ajou/procoding/myweb/controller/MyWebController.java new file mode 100644 index 0000000000000000000000000000000000000000..1376d3912ebb6e00496140fe812031b124f6bf1b --- /dev/null +++ b/src/main/java/com/ajou/procoding/myweb/controller/MyWebController.java @@ -0,0 +1,51 @@ +package com.ajou.procoding.myweb.controller; +import java.io.IOException; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + +import com.ajou.procoding.myweb.dto.MusicList; +import com.fasterxml.jackson.databind.ObjectMapper; + +@RestController +public class MyWebController { + + @GetMapping(value="/musicSearch/{term}") + public MusicList musicSearchByPath(@PathVariable String term) { + try { + String url = String.format("https://itunes.apple.com/search?term=%s&entity=album", term); + String response = restTemplate.getForObject(url, String.class); + //String response = restTemplate.getForObject("https://itunes.apple.com/search?term=aespa&entity=album", String.class); + ObjectMapper mapper = new ObjectMapper(); + MusicList list = mapper.readValue(response, MusicList.class); + System.out.println(list.getResultCount()); + return list; + } catch(IOException e) { + System.out.println(e.toString()); + } + return null; + } + + @GetMapping(value="/musicSearch") + public MusicList musicSearchByParam(@RequestParam String term){ + try { + String url = String.format("https://itunes.apple.com/search?term=%s&entity=album", term); + String response = restTemplate.getForObject(url, String.class); + //String response = restTemplate.getForObject("https://itunes.apple.com/search?term=aespa&entity=album", String.class); + ObjectMapper mapper = new ObjectMapper(); + MusicList list = mapper.readValue(response, MusicList.class); + System.out.println(list.getResultCount()); + return list; + } catch(IOException e) { + System.out.println(e.toString()); + } + return null; + } + //몇 줄 아닌거 같아 보이는데 많은 일을 함 + + RestTemplate restTemplate = new RestTemplate(); + +} \ No newline at end of file diff --git a/src/main/java/com/ajou/procoding/myweb/dto/MusicList.java b/src/main/java/com/ajou/procoding/myweb/dto/MusicList.java new file mode 100644 index 0000000000000000000000000000000000000000..ab6ca8e00e71ff1aba649625a5141e0f21812430 --- /dev/null +++ b/src/main/java/com/ajou/procoding/myweb/dto/MusicList.java @@ -0,0 +1,15 @@ +package com.ajou.procoding.myweb.dto; + +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; +import java.util.List; +import java.util.Map; + +@Getter +@Setter +@ToString +public class MusicList { + private Integer resultCount; + private List<Map<String, Object>> results; +}