Consume a public api for ny times

What you will learn here about Spring Boot

  • Consume a public api for ny times

Click on below link to download sample project.
Download Consume nytimes api sample project (144 downloads)

Consume a public api for ny times

Please follow the following steps to know how to consume a public api for ny times.
1)First create a simple maven project

2)Add Json as well as spring boot starter web dependency which is shown below

Spring Boot starter dependency

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

Json dependency

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20180130</version>
</dependency>

3) Create a Results class with title field which is shown below

public class Results{
	
    private String title;

    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
}

consume public api

4)Create a News class with following fields which is shown below

public class News {

	private String section;
    private Results[] results;
    private String title;


    public Results[] getResults() {
        return results;
    }

    public void setResults(Results[] results) {
        this.results = results;
    }

    public String getSection() {
        return section;
    }

    public void setSection(String section) {
        this.section = section;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }	
	
}

consume public api nytimes spring boot

5)Please create News service which is shown below

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service 
public class NewsService {

	private String apiKey = "gIIWu7P82GBslJAd0MUSbKMrOaqHjWOo";
	private RestTemplate restTemplate = new RestTemplate();
	private JSONObject jsonObject;
	private JSONArray jsonArray;
	private Results[] resultsArray;
	private News news=new News();
	
	public News getTopStories() throws Exception {
	
		List<News> topNewsStories = new ArrayList<>();
	    String Url = "https://api.nytimes.com/svc/topstories/v2/home.json?api-key=" + apiKey;
	
	    HttpHeaders headers = new HttpHeaders();
	    headers.setContentType(MediaType.APPLICATION_JSON);
	    HttpEntity entity = new HttpEntity(headers);
	    
	    ResponseEntity<Map> newsList = restTemplate.exchange(Url, HttpMethod.GET, entity, Map.class);
	
        if (newsList.getStatusCode() == HttpStatus.OK) {

            jsonObject = new JSONObject(newsList.getBody());
            jsonArray = jsonObject.getJSONArray("results");
            resultsArray = new Results[jsonArray.length()];
            
            for(int i=0; i<jsonArray.length(); i++) {
                news.setTitle(jsonArray.getJSONObject(i).get("title").toString());
                news.setSection(jsonArray.getJSONObject(i).get("section").toString());
                resultsArray[i]=new Results();
                resultsArray[i].setTitle(jsonArray.getJSONObject(i).get("title").toString());
                news.setResults(resultsArray);
                topNewsStories.add(news);
            }

        }
        return topNewsStories.get(0);
}

}

Consume developers api for ny times

6)Please create a News controller which is shown below

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class NewsController {

@Autowired
NewsService newsService;

	@GetMapping("/api/news/topstories")
	public News getTopNews() throws Exception {
	    return newsService.getTopStories();
	}

}

nytimes api consume using spring boot
7)Please run you spring boot application

8)Please go to browser and visit http://localhost:8080/api/news/topstories to see the response from nytimes api which is shown below
Consume a public api for ny times

You may also like...