카테고리 없음

스프링에서 외부 API 호출하기

숲별 2023. 12. 6. 18:14
728x90

 

https://jie0025.tistory.com/531

 

[Spring] 스프링에서 외부 API 호출하는 방법들

✅ 개요 외부 API를 사용해야하는 상황이 생겨서 호출 방법에 고민을 하고 있다. 빠른 사용/개발을 위해서는 사용하고자 하는 API의 클라이언트 라이브러리 자체를 이용해도 되지만, 이 경우 특정

jie0025.tistory.com

 

 

https://doohee94.tistory.com/19

 

Spring에서 외부 API 호출하기 1 (RestTemplate)

프로젝트에서 외부 api를 이용하여 로직을 짜는 부분을 맡게 되었다. Spring에서 외부 api를 호출하기 위해서는 RestTemplate이나 Webclient를 사용한다고 하여 관련 자료를 찾아보고 적용해 보았다. 사실

doohee94.tistory.com

 

에러메세지

경로 [/ai]의 컨텍스트 내의 서블릿 [appServlet]을(를) 위한 Servlet.service() 호출이, 근본 원인(root cause)과 함께...

https://threeyears.tistory.com/89

 

[스프링 오류 500] 서블릿 [appServlet]을(를) 위한 Servlet.init() 호출이 예외를 발생시켰습니다.

HomeController에서 @Inject ReplyService replyService; ReplyService를 등록 해주었는데. 바로 저 오류가 뜸 해결방법) 메시지는 memberController라는 빈을 생성하고자하는데 @Autowired할 때 빈생성에서 에러가 난다는

threeyears.tistory.com

위 글 시작부분을 보다가 @inject를 서비스 클래스 마다마다 줘야하나 싶어서 넣어줬더니 해결

 

 

https://thalals.tistory.com/266

 

[Spring] spring boot - 외부 Rest API json으로 받아오기(JAVA)

JAVA로 외부 api 값을 받아오는 방법은 3가지가 있다. httpurlconnection httpclient spring resttemplate 오늘은 이 3가지 방법에 대해서 알아보고 뭐를 사용할지 알아보고자 한다. + 결론은 3가지방법을 대략적

thalals.tistory.com

Could not extract response: no suitable HttpMessageConverter found for response type [class java.lang.Object] and content type [application/octet-stream]

하다가 이런 오류가 나왔고 콘텐트 타입 문제인 것 같았다.

 

위 블로그에서 헤더 바꾸는 코드 본 것 같아서

header.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));

 

위에 코드 추가했으나 여전히 같은 오류메세지를 뱉었다.

 

 

더 찾아보니

https://circlee7.medium.com/resttemplate-host-header-e30596ab04dd

 

RestTemplate… Host Header…

spring 의 RestTemplate 을 통해 Proxy 처럼 기능을 개발할 요건이 생겼다.

circlee7.medium.com

restTemplate.getInterceptors().add((request, body, execution) -> {
	                ClientHttpResponse response = execution.execute(request,body);
	                response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
	                return response;
	            });

이걸로 추가

패스

 

새로운 에러메세지 hi~

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON document: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')

 

https://ayoteralab.tistory.com/entry/Spring-BootError-JSON-parse-error-Unexpected-character

 

[Spring Boot][Error] JSON parse error: Unexpected character

API를 만들고 테스트를 하다 가끔 만날 수 있는 error입니다. 사실 error가 발생하면, 나오는 간단 설명을 읽어보면 어디서 왜 발생했는지 알 수 있습니다. Resolved [org.springframework.http.converter.HttpMessage

ayoteralab.tistory.com

 

...흠... 나는 request URL만 주면 되는데..

그냥 script단에서 ajax로 통신했던 걸 java로 해보는 중인데 이전에 성공했던 거랑 비교하니 Headers부분이 달랐다.

 

https://circlee7.medium.com/resttemplate-host-header-e30596ab04dd

 

RestTemplate… Host Header…

spring 의 RestTemplate 을 통해 Proxy 처럼 기능을 개발할 요건이 생겼다.

circlee7.medium.com

 

 

https://nnoco.tistory.com/235

 

Spring JSON message converter 삽질기..

아마 Moneycomb 프로젝트를 할 때였던 것 같다. 나는 그때도 JSON으로 메시지를 변환하기 위해, 책과 튜토리얼에서 빈번하게 등장하고, 다들 문제없이 쓰는 것으로 보이던 MappingJacksonHttpMessageConverter

nnoco.tistory.com

 

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON document: Unexpected character ('<' (code 60))

구글링해보니 결과가 JSON으로 반환되야하는데 <로 시작하는 XML형식으로 넘어와서 생기는 문제라고 했다.

도통 왜 XML로 넘어오는지 모르겠었는데 에러메세지를 xml로 넘겨주더라..

프로그램 로그보고 차이점 비교해보니

&outputFormat=application/json;%20subtype=geojson 넘겨줬는데

&outputFormat=application/json;%2520subtype%3Dgeojson 로 바뀌는 바람에

아래처럼 에러가 떴다.

&outputFormat=application/json; subtype%3Dgeojson로 넘겨주니 괜찮아졌다.

 

https://bepoz-study-diary.tistory.com/183

 

[Spring] UriComponentsBuilder 사용하기

UriComponentsBuilder는 여러개의 파라미터를 이용하여 URL를 작성할 때에 굉장히 편하게 작성할 수 있게끔 도와준다. docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/util/UriComponentsBuilder.html#fromPa

bepoz-study-diary.tistory.com

java.lang.IllegalArgumentException: protocol = http host = null

..?

 

 

String url = http://localhost:8880/orange/wfs?GDX=ai0001.xml

UriComponentsBuilder.newInstance()
                    .path(url)

 

이걸로 만들었는데 왜 

 

http:/localhost :8880/orange/wfs?GDX=ai0001.xml?~

가 만들어졌지..?

/하나 어디로 감..?

 

https://blog.naver.com/PostView.naver?blogId=aservmz&logNo=222322019981

 

[Spring] 38. 스프링에서 URI 조립을 위한 UriComponentsBuilder

#20210425 #SpringFramework #스프링프레임워크 #스프링 #스프링MVC #URI #UriComponents #Uri...

blog.naver.com