Sample code to call Yahoo! Shopping Product Search (v3) API in Spring RestTemplate

Overview

--Create a sample program that calls the API of Yahoo! Shopping Product Search (v3) using the HTTP client RestTemplate provided by Spring.

This environment

Source code

Source code list

├── build.gradle
└── src
    └── main
        └── java
            └── com
                └── example
                    ├── ErrorResponse.java
                    ├── ItemSearch.java
                    └── ResultSet.java

build.gradle

plugins {
  id 'application'
  id 'java'
}

group 'org.example'
version '0.0.1'
mainClassName = "com.example.ItemSearch"
sourceCompatibility = '11'

repositories {
  mavenCentral()
}

dependencies {

  //Required to use RestTemplate
  // https://mvnrepository.com/artifact/org.springframework/spring-web
  implementation 'org.springframework:spring-web:5.2.7.RELEASE'

  //Required for JSON and class mapping
  // https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
  implementation 'com.fasterxml.jackson.core:jackson-databind:2.11.0'
}

src/main/java/com/example/ItemSearch.java

package com.example;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpHeaders;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestClientResponseException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;

/**
 *shopping:Product search (v3)- Yahoo!Developer network
 * https://developer.yahoo.co.jp/webapi/shopping/shopping/v3/itemsearch.html
 */
public class ItemSearch {

  public static void main(String[] args) {

    try {
      String appid = args[0];
      String query = args[1];
      System.out.println("appid: " + appid); //Application ID
      System.out.println("query: " + query); //Search keyword

      //Search for products
      ResultSet rs = new ItemSearch().search(appid, query);
      for (ResultSet.Hit hit : rs.hits) {
        System.out.println("**************************************************");
        System.out.println("Product name: " + hit.name);
        System.out.println("Description of item: " + hit.description);
        System.out.println("catch copy: " + hit.headLine);
        System.out.println("76 x 76 size image URL: " + hit.image.small);
        System.out.println("Image URL of 146 x 146 size: " + hit.image.medium);
        System.out.println("Product URL: " + hit.url);
        System.out.println("price: " + hit.price);
      }
    } catch (Exception e) {
      System.out.println("Error occurred: " + e);
    }
  }

  /**
   *Search for products.
   * @param appid application ID
   * @param query Search keyword
   * @return search results
   * @throws Exception when an error occurs
   */
  public ResultSet search(String appid, String query) throws Exception {

    try {
      //Build URLs for API calls
      String baseurl = "https://shopping.yahooapis.jp/ShoppingWebService/V3/itemSearch";
      String url = UriComponentsBuilder.fromUri(new URI(baseurl))
        .queryParam("query", query) //Search keyword
        .queryParam("results", 2) //Up to 2 search results
        .build(false)
        .encode()
        .toUriString();
      System.out.println("URL: " + url);

      //Build HTTP request information
      HttpHeaders reqHeaders = new HttpHeaders();
      reqHeaders.set("User-Agent", "Yahoo AppID: " + appid); //Application ID
      RequestEntity req = RequestEntity.get(new URI(url))
        .headers(reqHeaders)
        .build();

      //Call the API to get the result
      RestTemplate restTemplate = new RestTemplate();
      ResponseEntity<ResultSet> res = restTemplate.exchange(req, ResultSet.class);
      return res.getBody();

    } catch (RestClientResponseException e) {

      //Output response status information
      int statusCode = e.getRawStatusCode();
      String statusText = e.getStatusText();
      System.out.println("RestClientResponseException.RawStatusCode: " + statusCode);
      System.out.println("RestClientResponseException.StatusText: " + statusText);

      //Output error information
      String body = e.getResponseBodyAsString();
      ObjectMapper mapper = new ObjectMapper();
      ErrorResponse errorResponse = mapper.readValue(body, ErrorResponse.class);
      System.out.println("Error message: " + errorResponse.error.message);

      throw e;

    } catch (Exception e) {
      throw e;
    }
  }
}

src/main/java/com/example/ResultSet.java

A class that represents the response of search results.

package com.example;

import java.util.List;

/**
 *search results.
 */
public class ResultSet {

  public List<Hit> hits;

  public static class Hit {

    // hits/name string Product name
    public String name;

    // hits/description string Item description
    public String description;

    // hits/headLine string catch phrase
    public String headLine;

    // hits/image
    public Image image;

    // hits/url string product url
    public String url;

    // hits/price integer price
    public int price;
  }

  public static class Image {

    // hits/image/small string 76 x 76 size image URL
    public String small;

    // hits/image/medium string 146 x 146 size image URL
    public String medium;
  }
}

src/main/java/com/example/ErrorResponse.java

A class that represents error response information.

package com.example;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
 *Error information.
 */
public class ErrorResponse {

  @JsonProperty("Error")
  public Error error;

  public static class Error {

    @JsonProperty("Message")
    public String message;
  }
}

Execution example

Specify the available application ID and search keyword "cat"

The response of the search result is mapped to the ResultSet class and the information is output.

$ gradle run -q --args="your_application_id cat"
appid: your_application_id
query:Cat
URL: https://shopping.yahooapis.jp/ShoppingWebService/V3/itemSearch?query=%E7%8C%AB&results=2
**************************************************
Product name:Cat toys fish robot cat electric toys cat automatic toys cat toys automatic cat toys electric cat toys fish
Description of item:[Pet toys] Electric fish robot Fish robot Toy cat Electric toy cat<br>[Automatic swimming function] Automatically turned on by the water sensor/Turn off. Designed with automatic robot fins<br>It starts moving as soon as it is placed in water, and automatically turns off when taken out, saving power.<br>[With LED light] Since the LED light is built-in, it glows in the dark. The shining and moving fish tickle the animal's hunting instinct<br>[Product usage scene] Automatic cat toys<br>[Applicable pets] Scottish Fold Maine Coon Ragdoll Russian Blue British Show Hair<br>Siberian Kijitora Stray Cat American Curl Scottish Sabatra Hachiware Neko-chan<br>
catch copy:An electric fish robot that stimulates the hunting instinct of cats
76 x 76 size image URL: https://item-shopping.c.yimg.jp/i/c/himawaridifang-store_robo-fish1
Image URL of 146 x 146 size: https://item-shopping.c.yimg.jp/i/g/himawaridifang-store_robo-fish1
Product URL: https://store.shopping.yahoo.co.jp/himawaridifang-store/robo-fish1.html
price: 880
**************************************************
Product name:Cat harness, cat leash, cat supplies, pet supplies
Description of item:As an escape measure<br><br>Cats often panic and try to escape or rampage when they are moved unfamiliarly, such as when they go to the hospital or move. In such a case, it is very safe not only to put it in the pet cage, but also to put it in the pet cage after attaching the harness. Also, in the unlikely event that you have to evacuate due to a major disaster, it will help prevent escape, so more and more owners are keeping it in their disaster prevention goods.<br><br>For a walk<br><br>脱走防止目的だけでなく、猫のFor a walk使用するのもおすすめです。ハーネスは猫の身体の動きを邪魔しにくいので、のびのびと運動できて良いストレス発散になることでしょう。ただし、猫の性格によっては外に連れ出すことがかえってストレスとなる場合もありますので、お散歩はくれぐれも猫の様子をみつつ、交通状況などにも気を配りながら行ってください。<br><br>There are four color variations: red, blue, black, and pink.<br>Made of nylon<br><br>Neck circumference 16-26 cm<br>Girth 26-36 cm<br>Lead length 110 cm<br><br>Search keywords: cat leash, cat harness, cat leash, point digestion, free shipping
catch copy:Cat harness cat leash cat leash cat collar
76 x 76 size image URL: https://item-shopping.c.yimg.jp/i/c/sam-store_0030
Image URL of 146 x 146 size: https://item-shopping.c.yimg.jp/i/g/sam-store_0030
Product URL: https://store.shopping.yahoo.co.jp/sam-store/0030.html
price: 599

Specify an unusable application ID and search keyword "cat"

The error response is mapped to the ErrorResponse class and the information is output.

$ gradle run -q --args="invalid_application_id cat"
appid: invalid_application_id
query:Cat
URL: https://shopping.yahooapis.jp/ShoppingWebService/V3/itemSearch?query=%E7%8C%AB&results=2
RestClientResponseException.RawStatusCode: 403
RestClientResponseException.StatusText: Forbidden
Error message: Your Request was Forbidden
Error occurred: org.springframework.web.client.HttpClientErrorException$Forbidden: 403 Forbidden: [{
"Error" : {
"Message" : "Your Request was Forbidden"
}
} ... (512 bytes)]

Reference material

-Shopping: Product Search (v3) -Yahoo ! Developer Network -[RestTemplate \ (Spring Framework 5 \ .2 \ .7 \ .RELEASE API ) -Javadoc Japanese translation](https://spring.pleiades.io/spring/docs/5.2.7.RELEASE/javadoc- api / org / springframework / web / client / RestTemplate.html) -[UriComponentsBuilder \ (Spring Framework 5 \ .2 \ .7 \ .RELEASE API ) -Javadoc Japanese translation](https://spring.pleiades.io/spring/docs/5.2.7.RELEASE/javadoc- api / org / springframework / web / util / UriComponentsBuilder.html)

Recommended Posts

Sample code to call Yahoo! Shopping Product Search (v3) API in Spring RestTemplate
Sample code to call Yahoo! Shopping Product Search (v3) API with HTTP Client API officially introduced from Java 11
Sample code to call the Yahoo! Local Search API in Java
[Java] I tried to implement Yahoo API product search
How to call and use API in Java (Spring Boot)
[Java] New Yahoo! Product Search API Implementation
03. I sent a request from Spring Boot to the zip code search API
Demo code to recursively search directories in Files.walkFileTree
Sample code to convert List to List <String> in Java Stream
RESTful API multi-module sample in IntelliJ + Jersey + Spring Framework
Try to implement using Rakuten product search API (easy)
Introduce swagger-ui to REST API implemented in Spring Boot
Minimum configuration sample of RESTful API in Jersey + Spring Framework
Try hitting the zip code search API with Spring Boot
Create API to send and receive Json data in Spring