[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.


//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);

HttpURLConnection con = (HttpURLConnection)conUrl.openConnection();

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.


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

//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();

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
java1.8 new features
Try to implement using Rakuten product search API (easy)
Rails hashtag search implementation
Interpreter implementation in Java
Java 12 new feature summary
Check Java toString () implementation
Java 13 new feature summary
[Java] Stream API / map
Implementation of search function
[Note] Java: String search
Java8 Stream API practice
Heapsort implementation (in java)
Zabbix API in Java
What's new in Java 8
Java 14 new feature summary
What's new in Java 9,10,11