[Java] New Yahoo! Product Search API Implementation

Note that Yahoo!'S product search API will have new specifications. https://developer.yahoo.co.jp/webapi/shopping/shopping/v3/itemsearch.html

The structure of JSON has changed due to the need to encode the search word. We will use Jackson to handle JSON. Please check the introduction by yourself.

things to do

  1. Send the search word received from HTML to the API.
  2. Receive JSON, extract necessary information and forward it.

1. Send the search word received from HTML to the API.

As a change from the old specification, UTF-8 encoding is required.

java


//Application ID
final String appID = "Application ID";

//Search word UTF-8 encoding
String query = request.getParameter("searchWord");
String encodedQuery = URLEncoder.encode(query, "UTF-8");

//URL creation
String url = "https://shopping.yahooapis.jp/ShoppingWebService/V3/itemSearch"
               + "?appid=" + appID + "&query=" + encodedQuery;
URL conUrl = new URL(url);

//Connect
HttpURLConnection con = (HttpURLConnection)conUrl.openConnection();
con.connect();

2. Receive JSON, extract necessary information and forward it.

Note that the JSON structure is different from the old specifications. This time, we will extract the top 10 product names and prices from the search results.

java


//Read json string
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
String json = br.readLine();
br.close();

//Convert json string to JsonNode
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(json);

//ArrayList that stores JavaBeans
ArrayList<DataBeans> list = new ArrayList<>();

//Extract information for 10 items and store it in the list
for(int i = 0; i < 10; i++){
  //Since the value of hits is an array, specify the element number as an integer.
  String name = node.get("hits").get(i).get("name").textValue();
  int price = node.get("hits").get(i).get("price").asInt();
  
  //Set information in JavaBeans
  DataBeans bean = new DataBeans();
  bean.setName(name);
  bean.setPrice(price);

  list.add(bean);
}
request.setAttribute("resultData", list);
request.getRequestDispatcher("searchResult.jsp").forward(request, response);

Recommended Posts

[Java] New Yahoo! Product Search API Implementation
[Java] I tried to implement Yahoo API product search
[Java] Create something like a product search API
Sample code to call the Yahoo! Local Search API in Java
Sample code to call Yahoo! Shopping Product Search (v3) API with HTTP Client API officially introduced from Java 11
Java Stream API
[Java] Get images with Google Custom Search API
Sample code to call Yahoo! Shopping Product Search (v3) API in Spring RestTemplate
java1.8 new features
Call Amazon Product Advertising API 5.0 (PA-API v5) in Java
Try to implement using Rakuten product search API (easy)
Rails hashtag search implementation
Java 12 new feature summary
Check Java toString () implementation
Java 13 new feature summary
[Java] Stream API / map
Implementation of search function
Java8 Stream API practice
Heapsort implementation (in java)
Zabbix API in Java
Search function [copype implementation]
What's new in Java 8
Java 10 new feature summary
Java 14 new feature summary
What's new in Java 9,10,11