반응형

[ Spring ] 문제해결 no suitable constructor found, can not deserialize from Object value 


API로 JSON 데이터 받아와 모델에 매핑시키는 부분에서 다음과 같은 에러가 발생하였다.


org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not construct instance of beomcess.coin.contractor.entity.Bittrex$BittrexCoin: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)

 at [Source: java.io.PushbackInputStream@423b2b62; line: 1, column: 41] (through reference chain: beomcess.coin.contractor.entity.Bittrex["result"]->java.util.ArrayList[0]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of beomcess.coin.contractor.entity.Bittrex$BittrexCoin: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)

 at [Source: java.io.PushbackInputStream@423b2b62; line: 1, column: 41] (through reference chain: beomcess.coin.contractor.entity.Bittrex["result"]->java.util.ArrayList[0])

at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:240)

at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:225)

at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:95)

at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:655)


쉽게 말해 API 응답으로 받아와 JSON 데이터가 내가 매핑하고자 하는 Model에 알맞지 않다는 것이다...


API호출 결과 날아오는 JSON의 형태는 다음과 같았다.



내가 받고자 했던 JAVA MODEL의 형태는 다음과 같았다.


문제는 Result Array 내부에 있는 데이터들을 매핑해 오지 못해생겼던 것 같다.



일단 해결은 내부의 BittrexCoin 클래스에 static을 선언해 해결하였다.



이렇게 inner class를 static으로 변경하고 나니 정상적으로 데이터를 받아 올 수 있었다.


static 지시사의 역할은 메소드나 변수를 메모리에 로딩해서 다른 클래스가 이 클래스의 인스턴스를


생성하지 않고서도 사용할 수 있게 해주는 목적이다. 


RestTemplate으로 요청을 날리면서 동시에 내가 원하는 모델에 매핑해서 데이터를 가져오는데 static을 선언해주지 않았을 경우에


inner class인스턴스를 못만들어내서 매핑이 되지 않는 것 같다. 더 정확한 원인까지는 잘 모르겠다...


나중에 시간되면 더 파고들어볼만한 이슈인 것 같다.


혹시 원인에 대해 아시는분이 계시다면 댓글남겨주시면 감사하겠습니다. 







반응형
반응형

안만져본 툴들에서 기본적으로 데이터를 내가 원하는 방식으로 select해오는 것도 쉽지 않구나...


정리가 잘되있는것 같아서 공유


1. [ 엘라스틱서치 쿼리 DSL 기초 ]

https://bakyeono.net/post/2016-08-20-elasticsearch-querydsl-basic.html



2.  [ 엘라스틱서치 검색을 해보자 ] 

https://iju707.gitbooks.io/elasticsearch/content/_executing_searches.html



! 위의 문서에서는 GET 방식으로 조회하였으나, curl에서 GET의 Body를 지원하지만 실제 REST/HTTP 스펙상 GET은


Body를 사용하지 않는 것이 많기 때문에 POST로 사용할 것을 권장한다.




반응형
반응형


엘라스틱서치(ElasticSearch)에서 제공하는 API를 통해 데이터를 조회할 때 한번쯤은 'pretty?=true'가 붙어있는 것을 보았을 것이다.


단순히 search의 결과를 보기좋게 출력해주는 파라미터이다. 



[ 'pretty?=true' 사용하지 않고 조회했을 경우 ] 


> curl -XGET http://localhost:9200/logs/_search




[ 'pretty?=true' 사용 ] 


> curl -XGET http://localhost:9200/logs/_search?pretty=true




이미지를 보면 알겠지만 'pretty?=true' 사용하여 조회하는게 훨씬 결과를 보기좋게 볼 수 있다.


콘솔화면에서 데이터 조회시 유용하게 사용할 수 있을 것 같다.

반응형
반응형



@QueryParam ? @PathParam?

흔히 API설계하실 때 특히 GET 방식의 URI 만드실 때 쿼리 파라미터로 설계할지 패스파라미터로 설계하실지 고민들하실텐데요.


저또한 많이 고민하다가 관련해서 열띤 토론?을 나눈 stackoverflow글이 있어 공유합니다.



---------------------------------------------------------------------------------------------------------------------------------------


This is what I do.

If there is a scenario to retrieve a record based on id, for example you need to get the details of the employee whose id is 15, then you can have resource with @PathParam.

GET /employee/{id}

If there is a scenario where you need to get the details of all employees but only 10 at a time, you may use query param

GET /employee?start=1&size=10

This says that starting employee id 1 get ten records.

To summarize, use @PathParam for retrieval based on id. 

User @QueryParam for filter or if you have any fixed list of options that user can pass.


[ 자세한건 참고 : StackOverflow]

http://stackoverflow.com/questions/11552248/when-to-use-queryparam-vs-pathparam

-------------------------------------------------------------------------------------------

[ 결 론 ]

결국에는 특정 정보를 가지고 올 때는  PATH PARAMETER를 데이터에 특정 조건(필터)를 주거나 SORT를 해서 가지고 오고자 할 경우에는 쿼리파라미터를 사용하면 좀 더 깔끔하게 API 를 설계하실 수 있을 것 같습니다.

감사합니다.

 




반응형

+ Recent posts