• Register
0 votes
1.8k views

Problem :

I am trying to use the curl command as follows:

curl -u 591bf65f50057469f10b5fd9:0cf17f9b03d056ds0e11e48497e506a2 https://backend.tdk.com/api/devicetypes/59147fd79e93s12e61499ffe/messages

But I am getting a JSON response as follows:

{"data":[{"device":"18SE62","time":1494516023,"data":"3235","snr":"36.72",...

I have saved the response on the txt file and parse it using jackson, and everything is fine as below:

ObjectMapper mymapper = new ObjectMapper();
File myfile = new File(getClass().getResource("/result.json").getFile());
MessageList mymessageList = mymapper.readValue(myfile, MessageList.class);

Also I am assuming that I should get the same result using the RestTemplate but that's not the case as below:

RestTemplate myrestTemplate = new RestTemplate();
MessageList mymessageList = 
myrestTemplate.getForObject("http://592693f43c87815f9b8145e9:f099c85d84d4e325a2186c02bd0caeef@backend.tdk.com/api/devicetypes/591570373c87894b4eece34d/messages", MessageList.class);

But I got an error instead as below:

Exception in thread "main" org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.tdk.domain.backend.MessageList] and content type [text/html;charset=iso-8859-1]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:88)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:711)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:520)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:114)
at com.tdk.controllers.restful.client.RestTemplateExample.main(RestTemplateExample.java:14)

I also tried to set the contentType as follows:

HttpHeaders myheaders = new HttpHeaders();
myheaders.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> myentity = new HttpEntity<String>("parameters", myheaders);
MessageList mymessageList = myrestTemplate.getForObject(myurl, myentity, MessageList.class);

however then I got a compilation error as below

The method getForObject(String, Class<T>, Object...) in the type RestTemplate is not applicable for the arguments (String, HttpEntity<String>,Class<MessageList>)
6 5 3
7,540 points

1 Answer

0 votes

Solution :

I was also having the very similar problem, and it turned out to be the very simple; my client was not including the Jackson dependency, even though my code all compiled correctly, but the auto-magic converters for the JSON were not being included in it.

To summarize, I just added a Jackson dependency to my pom.xml and it just worked as follows :

<dependency>
<groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.5.1</version>
</dependency>
9 7 4
38,600 points

Related questions

1 vote
1 answer 1.1K views
1.1K views
Problem : I am beginner to spring + hibernate. Recently I have created the application with the use of spring + hibernate but I always face below error. I am trying to work on it for the very first time. Below is the error for your ... ) at coreservlets.StudentDAOImpl.create(StudentDAOImpl.java:19) at coreservlets.MainApp.main(MainApp.java:14) Can somebody help me in overcoming above error?
asked Mar 28 morrisBson 3.2k points
0 votes
2 answers 165 views
165 views
Problem : I have created entities, services and JUnit tests for services using Spring and Hibernate for my project. All of these are working. After that I added spring-mvc to it to make this web application but while trying to make ... follows ..Could not autowire field: public pl.com.radzikowski.webmail.service.IMailManager pl.com.radzikowski.webmail.controller.HomeController.mailManager...
asked Nov 20, 2019 peterlaw 6.9k points
0 votes
1 answer 1.6K views
1.6K views
Problem : I am trying to write Spring boot code The Program is running but I am getting following error: This application has no explicit mapping for /error, so you are seeing this as a fallback. How to fix this error?
asked Nov 22, 2019 peterlaw 6.9k points
0 votes
2 answers 367 views
367 views
Problem : I am new to the Spring and JPA, wasted 10 days and no result with the searching on internet. I want to save the object to SQL SERVER, my connection is correct but when I write .flush() I get below exception &ldquo;nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress&rdquo;
asked Jan 21 jwilliam 3.9k points
0 votes
1 answer 217 views
217 views
Problem : I am new to Spring and JPA I want to save object to SQL SERVER, connection is correct but when I write .flush() I get the exception as below javax.persistence.TransactionRequiredException: no transaction is in progress
asked Oct 22, 2019 peterlaw 6.9k points