m_shige1979のときどきITブログ

プログラムの勉強をしながら学習したことや経験したことをぼそぼそと書いていきます

Github(変なおっさんの顔でるので気をつけてね)

https://github.com/mshige1979

Spring Bootで設定ファイルのデータを取得する

設定ファイルというと

iniファイルとかcsvファイルとか独自に用意したプロパティファイルとかあります。
プログラム内の固定値とかではなく実行環境(開発、本番)で設定値が変化する場合に切り替えたいもの

Spring Bootの場合は

application.properties
が基本らしい
他のファイルとしてはymlもあるみたいですね。

設定する情報とは

サーバのポートやデータベースの接続情報などを設定して使うことが基本です。
ですが独自の設定情報を保持したいこともあります。

ので独自の設定値を取得できるかテストします。

サンプル

application.properties
# sample
sample.test1=aaaaaaaaa
sample.test2=222222222
sample.test3=hogehoge

こんな感じ…

取得処理

SampleController.java
package com.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SampleController {
	
	@Autowired
	private Environment eivironment;
	
	@RequestMapping(value = "/sample/getProperty/{key}", method = { RequestMethod.GET } )
	public String test1(@PathVariable String key) {
		return eivironment.getProperty("sample." + key);
	}
	
}

※ Environmentを使用する。

結果

f:id:m_shige1979:20170109222612p:plain
取得できました
設定ファイル名は仕方がないので別途定数で管理しましょう。

今回はここまでですm(_ _)m

Javaで形態素解析ライブラリのkuromojiを使ってみた

形態素解析とかいうので

文字列を単語別に分割できるらしい
全文検索とか名刺だけを取得したい場合とかに使えそう

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example.sample</groupId>
  <artifactId>SampleKaiseki01</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>
    
    <!-- https://mvnrepository.com/artifact/com.atilika.kuromoji/kuromoji-core -->
    <dependency>
      <groupId>com.atilika.kuromoji</groupId>
      <artifactId>kuromoji-ipadic</artifactId>
      <version>0.9.0</version>
    </dependency>
  
  </dependencies>
  
</project>

サンプル

Sample01.java
package com.example.sample;

import java.util.List;

import com.atilika.kuromoji.ipadic.Token;
import com.atilika.kuromoji.ipadic.Tokenizer;

public class Sample01 {
	
	public static void main(String[] args) {
		
		Tokenizer tokenizer = new Tokenizer() ;
		
		tokenDisplay(tokenizer.tokenize("山田 太郎"));
		tokenDisplay(tokenizer.tokenize("りんごはおいしい"));
		tokenDisplay(tokenizer.tokenize("この仕事は大変そうです。"));
		tokenDisplay(tokenizer.tokenize("扇風機が動かないんですけど急いで直してくれない?"));
	}
	
	private static void tokenDisplay(List<Token> tokens) {
		for (Token token : tokens) {
			System.out.println(token.getSurface() + "\t" + token.getAllFeatures());
	    	System.out.println(token.getPartOfSpeechLevel1());
	    	System.out.println(token.getPartOfSpeechLevel2());
        }
	}
}

結果

山田	名詞,固有名詞,人名,姓,*,*,山田,ヤマダ,ヤマダ
名詞
固有名詞
 	記号,空白,*,*,*,*, , , 
記号
空白
太郎	名詞,固有名詞,人名,名,*,*,太郎,タロウ,タロー
名詞
固有名詞
りんご	名詞,一般,*,*,*,*,りんご,リンゴ,リンゴ
名詞
一般
は	助詞,係助詞,*,*,*,*,は,ハ,ワ
助詞
係助詞
おいしい	形容詞,自立,*,*,形容詞・イ段,基本形,おいしい,オイシイ,オイシイ
形容詞
自立
この	連体詞,*,*,*,*,*,この,コノ,コノ
連体詞
*
仕事	名詞,サ変接続,*,*,*,*,仕事,シゴト,シゴト
名詞
サ変接続
は	助詞,係助詞,*,*,*,*,は,ハ,ワ
助詞
係助詞
大変	名詞,形容動詞語幹,*,*,*,*,大変,タイヘン,タイヘン
名詞
形容動詞語幹
そう	名詞,接尾,助動詞語幹,*,*,*,そう,ソウ,ソー
名詞
接尾
です	助動詞,*,*,*,特殊・デス,基本形,です,デス,デス
助動詞
*
。	記号,句点,*,*,*,*,。,。,。
記号
句点
扇風機	名詞,一般,*,*,*,*,扇風機,センプウキ,センプーキ
名詞
一般
が	助詞,格助詞,一般,*,*,*,が,ガ,ガ
助詞
格助詞
動か	動詞,自立,*,*,五段・カ行イ音便,未然形,動く,ウゴカ,ウゴカ
動詞
自立
ない	助動詞,*,*,*,特殊・ナイ,基本形,ない,ナイ,ナイ
助動詞
*
ん	名詞,非自立,一般,*,*,*,ん,ン,ン
名詞
非自立
です	助動詞,*,*,*,特殊・デス,基本形,です,デス,デス
助動詞
*
けど	助詞,接続助詞,*,*,*,*,けど,ケド,ケド
助詞
接続助詞
急い	動詞,自立,*,*,五段・ガ行,連用タ接続,急ぐ,イソイ,イソイ
動詞
自立
で	助詞,接続助詞,*,*,*,*,で,デ,デ
助詞
接続助詞
直し	動詞,非自立,*,*,五段・サ行,連用形,直す,ナオシ,ナオシ
動詞
非自立
て	助詞,接続助詞,*,*,*,*,て,テ,テ
助詞
接続助詞
くれ	動詞,非自立,*,*,一段・クレル,未然形,くれる,クレ,クレ
動詞
非自立
ない	助動詞,*,*,*,特殊・ナイ,基本形,ない,ナイ,ナイ
助動詞
*
?	記号,一般,*,*,*,*,?,?,?
記号
一般

所感

使い所がむずかしいな…
なんかいい感じのところで区切ってくれるとありがたいけどまだそこまで精度が高いわけではなさそう
名詞とかで区切る方法がいいのかも…

AWSのS3をMavenのパッケージを使用してアップロード、ダウンロードを実施

キーアクセスには

ファイルの場合と設定ファイルなどよりキーの値より取得する2パターンがある
別途定数などに入れて扱いたいのでちょっと別の方法をやる

BasicAWSCredentials

パラメータに直接キーとシークレットキーを指定するのでキーの元がわかりやすい

pom.xml

  <dependencies>
    
    <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk -->
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-java-sdk</artifactId>
      <version>1.11.76</version>
    </dependency>
  
  </dependencies>

アップロードサンプル

Sample01.java
package com.example.awssample;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.TransferManagerConfiguration;

public class Sample01 {
    
    // アクセスキー
    private static final String ACCESS_KEY = "アクセスキーの文字列";
    // シークレットアクセスキー
    private static final String SECRET_ACCESS_KEY = "シークレットアクセスキーの文字列";
    // 分割サイズ
    private static final long PART_SIZE = 5 * 1024L * 1024L;
    
    public static void main(String[] args) throws IOException {
        
        byte[] b;
        
        // ファイルを読み込み
        File file = new File("画像ファイパス/sample1.jpg");
        InputStream inputStream = new FileInputStream(file);
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        int data;
        while ((data = inputStream.read()) != -1) {
            bout.write(data);
        }
        b = bout.toByteArray();
        
        // 長さ
        long contentLength = b.length;
        
        // 設定情報
        BasicAWSCredentials credentials;
        credentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_ACCESS_KEY); 
        
        // AWSのクライアント取得
        AmazonS3 s3 = new AmazonS3Client(credentials);
        
        // バケット名
        String bucketName = "awsuploadsample";
        
        // アップロードキー名
        String key = UUID.randomUUID().toString();
        
        // パスを指定することで指定の場所へ配置できる
        String objectPath = "sample1/" + key;
        
        // TransferManagerを利用
        TransferManager manager = new TransferManager(s3);
     
        // 分割サイズを設定
        TransferManagerConfiguration c = new TransferManagerConfiguration();
        c.setMinimumUploadPartSize(PART_SIZE);
        manager.setConfiguration(c);
        
        // メタデータに分割したデータのサイズを指定
        ObjectMetadata putMetaData = new ObjectMetadata();
        putMetaData.setContentLength(contentLength);
        
        // upload
        PutObjectRequest object = new PutObjectRequest(bucketName, objectPath, new ByteArrayInputStream(b), putMetaData);
        s3.putObject(object);
    }
    
}

ダウンロードサンプル

Sample02.java
package com.example.awssample;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.S3Object;


public class Sample02 {
    
    // アクセスキー
    private static final String ACCESS_KEY = "アクセスキーの文字列";
    // シークレットアクセスキー
    private static final String SECRET_ACCESS_KEY = "シークレットアクセスキーの文字列";
    // 分割サイズ
    private static final long PART_SIZE = 5 * 1024L * 1024L;
    
    public static void main(String[] args) throws IOException {
        
        // 設定情報
        BasicAWSCredentials credentials;
        credentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_ACCESS_KEY); 
                
        // AWSのクライアント取得
        AmazonS3 s3 = new AmazonS3Client(credentials);
                
        // バケット名
        String bucketName = "awsuploadsample";
                
        // ダウンロードキー名
        String key = "41dbccd0-c33b-44f1-9635-37a73f42b3e7";
         // パスを指定することで指定の場所へ配置できる
        String objectPath = "sample1/" + key;
        
        // リクエストを取得
        GetObjectRequest request = new GetObjectRequest(bucketName, objectPath);
        
        // objectを取得
        S3Object object = s3.getObject(request);
        
        // データ取得
        InputStream is = object.getObjectContent();
        FileOutputStream fos = new FileOutputStream("/Users/matsumotoshigeharu/Documents/sample2.jpg");
        
        // ファイル出力
        byte[] buffer = new byte[5 * 1024 * 1024];
        int readSize = -1;
        while( (readSize = is.read(buffer, 0, buffer.length)) != -1) {
          fos.write(buffer, 0, readSize);
        }
        fos.flush();
    }
    
}

AWSのS3へJavaのSDKを使用してアップロード

AWSへプログラムよりアクセスする

なんかやることが多くなってきた

準備

AWSのIAMでS3用のユーザーを作成し、アクセスキーとシークレットアクセスキーを取得

eclipseへ機能を追加

新規ソフトウェアのインストールで「https://aws.amazon.com/eclipse」を設定

f:id:m_shige1979:20170108152855p:plain

アクセスキーとシークレットアクセスキーを設定する

f:id:m_shige1979:20170108153553p:plain

設定を変更して東京リージョンへする

f:id:m_shige1979:20170108154216p:plain

eclipse

プロジェクトで「AWS Java Project」で作成

f:id:m_shige1979:20170108155455p:plain

Amazon S3 Sample

f:id:m_shige1979:20170108155645p:plain

サンプル
package com.example.awssample;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.UUID;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectSummary;

public class S3Sample {

    public static void main(String[] args) throws IOException {

        // eclipseの設定ファイルのパスよりアクセスキー、シークレットアクセスキーを参照
        AWSCredentials credentials = null;
        try {
            credentials = new ProfileCredentialsProvider("default").getCredentials();
        } catch (Exception e) {
            throw new AmazonClientException(
                    "Cannot load the credentials from the credential profiles file. " +
                    "Please make sure that your credentials file is at the correct " +
                    e);
        }

        AmazonS3 s3 = new AmazonS3Client(credentials);
        Region usWest2 = Region.getRegion(Regions.AP_NORTHEAST_1);
        s3.setRegion(usWest2);

        String bucketName = "awsuploadsample";
        String key = UUID.randomUUID().toString();

        System.out.println("===========================================");
        System.out.println("Getting Started with Amazon S3");
        System.out.println("===========================================\n");

        try {
            // バケット名
            System.out.println("Creating bucket " + bucketName + "\n");
            
            // バケット一覧
            System.out.println("Listing buckets");
            for (Bucket bucket : s3.listBuckets()) {
                System.out.println(" - " + bucket.getName());
            }
            System.out.println();

            // ファイルアップロード
            System.out.println("Uploading a new object to S3 from a file\n");
            s3.putObject(new PutObjectRequest(bucketName, key, createSampleFile()));

            // ・ダウンロード
            System.out.println("Downloading an object");
            S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));
            System.out.println("Content-Type: "  + object.getObjectMetadata().getContentType());
            displayTextInputStream(object.getObjectContent());

            // 一覧を表示
            System.out.println("Listing objects");
            ObjectListing objectListing = s3.listObjects(new ListObjectsRequest()
                    .withBucketName(bucketName)
                    .withPrefix("My"));
            for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                System.out.println(" - " + objectSummary.getKey() + "  " +
                                   "(size = " + objectSummary.getSize() + ")");
            }
            System.out.println();

            // アップロードファイルの削除
            //System.out.println("Deleting an object\n");
            //s3.deleteObject(bucketName, key);
            
        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException, which means your request made it "
                    + "to Amazon S3, but was rejected with an error response for some reason.");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
        } catch (AmazonClientException ace) {
            System.out.println("Caught an AmazonClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with S3, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message: " + ace.getMessage());
        }
    }

    /**
     * Creates a temporary file with text data to demonstrate uploading a file
     * to Amazon S3
     *
     * @return A newly created temporary file with text data.
     *
     * @throws IOException
     */
    private static File createSampleFile() throws IOException {
        File file = File.createTempFile("aws-java-sdk-", ".txt");
        file.deleteOnExit();

        Writer writer = new OutputStreamWriter(new FileOutputStream(file));
        writer.write("abcdefghijklmnopqrstuvwxyz\n");
        writer.write("01234567890112345678901234\n");
        writer.write("!@#$%^&*()-=[]{};':',.<>/?\n");
        writer.write("01234567890112345678901234\n");
        writer.write("abcdefghijklmnopqrstuvwxyz\n");
        writer.close();

        return file;
    }

    /**
     * Displays the contents of the specified input stream as text.
     *
     * @param input
     *            The input stream to display as text.
     *
     * @throws IOException
     */
    private static void displayTextInputStream(InputStream input) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
        while (true) {
            String line = reader.readLine();
            if (line == null) break;

            System.out.println("    " + line);
        }
        System.out.println();
    }

}

結果

f:id:m_shige1979:20170108161508p:plain


Spring Bootで画像を縮小して表示

画像を縮小する

そもそも画像制御とかプログラムでほとんど書かないけどね

実装

Image01Controller.java
package com.example.controller;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.core.io.ResourceLoader;
import javax.imageio.ImageIO;

@RestController
public class Image01Controller {
	
	@Autowired
	ResourceLoader resourceLoader;
	private int c;
		
	@ResponseBody
	@RequestMapping(value = "/getImageReducing/{id}", method = {RequestMethod.GET })
	public HttpEntity<byte[]> getImageReducing(@PathVariable String id) throws IOException {
		
		// リソースファイルを読み込み
		Resource resource = resourceLoader.getResource("classpath:" + "/static/image/" + id + ".jpg");
		InputStream image = resource.getInputStream();
		
		// BufferedImageへ設定
		BufferedImage bufferedImage1 = ImageIO.read(image);
		BufferedImage bufferedImage2 = null;
		
		// 変換情報を設定
		int width = 300;
		int height = 250;
		Image image1 = bufferedImage1.getScaledInstance(width, height, Image.SCALE_DEFAULT);
		bufferedImage2 = new BufferedImage(image1.getWidth(null), image1.getHeight(null), BufferedImage.TYPE_INT_RGB);
		
		// 縮小処理
		Graphics graphics2 = null;
		try {
			graphics2 = bufferedImage2.getGraphics();
			int x = 0;
			int y = 0;
			graphics2.drawImage(bufferedImage1, x, y, width, height, null);
		} finally {
			graphics2.dispose();
		}
		
		// byteへ変換
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		ImageIO.write(bufferedImage2, "jpg", bout);
		byte[] b = bout.toByteArray();
		
		// レスポンスデータとして返却
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.IMAGE_JPEG);
		headers.setContentLength(b.length);
		return new HttpEntity<byte[]>(b, headers);
	}
	
}

Spring BootでRestControllerで画像を出力する

簡単にresources配下にあるファイルを画面に表示する

Spring Bootの場合は@RestControllerを返す場合は指定のデータを返却するような感じになるので返り値をバイナリ形式で返してみる。

やること

  • 画像を読み込み
  • byte[]へ変換して設定
  • jpegデータとして返却

実装

pom.xml
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
Image01Controller.java
package com.example.controller;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.core.io.ResourceLoader;

@RestController
public class Image01Controller {
	
	@Autowired
	ResourceLoader resourceLoader;
	
	@ResponseBody
	@RequestMapping(value = "/getImage/{id}", method = {RequestMethod.GET })
	public HttpEntity<byte[]> getImage(@PathVariable String id) throws IOException {
		
		// リソースファイルを読み込み
		Resource resource = resourceLoader.getResource("classpath:" + "/static/image/" + id + ".jpg");
		InputStream image = resource.getInputStream();
		
		// byteへ変換
		byte[] b = IOUtils.toByteArray(image);
		
		// レスポンスデータとして返却
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.IMAGE_JPEG);		
		headers.setContentLength(b.length);
		return new HttpEntity<byte[]>(b, headers);
	}
	
}

わかったこと

  • RestControllerでバイナリを返すことができる
  • リソースファイルにアクセスするにはResourceLoaderを使用する
  • バイナリデータを返すことで画像などを表示することが可能

今回はここまで…

おまけ

ByteArrayOutputStreamでbyte[]変換した場合
package com.example.controller;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.core.io.ResourceLoader;

@RestController
public class Image01Controller {
	
	@Autowired
	ResourceLoader resourceLoader;
	private int c;
	
	@ResponseBody
	@RequestMapping(value = "/getImage/{id}", method = {RequestMethod.GET })
	public HttpEntity<byte[]> getImage(@PathVariable String id) throws IOException {
		
		// リソースファイルを読み込み
		Resource resource = resourceLoader.getResource("classpath:" + "/static/image/" + id + ".jpg");
		InputStream image = resource.getInputStream();
		
		// byteへ変換
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		int c;
		while ((c = image.read()) != -1) {
			bout.write(c);
		}
		byte[] b = bout.toByteArray();
		
		// レスポンスデータとして返却
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.IMAGE_JPEG);
		headers.setContentLength(b.length);
		return new HttpEntity<byte[]>(b, headers);
	}
	
}

Spring BootのRestTemplateでバイナリデータを取得する

RestTemplate

Spring BootのAPI処理クラスと思われる

Rest APIの場合は基本JSONなんだけど

APIの結果としてはバイナリデータを受け取る場合もあるので取得したい

動作用API

LINE APIのGet ContentAPIを使用
https://devdocs.line.me/ja/?shell#get-content

実装サンプル

pom.xml
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
App
** RestTemplate
Spring BootのAPI処理クラスと思われる

** 実装サンプル
*** App
>|java|
package com.example;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;

import org.apache.commons.io.IOUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class SampleSpringResttemplate01Application {

    public static void main(String[] args) throws IOException {
        SpringApplication.run(SampleSpringResttemplate01Application.class, args);
        
        // インスタンス生成
        RestTemplate restTemplate = new RestTemplate();

        // メッセージコンバーター設定
        restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());

        // ヘッダー設定
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM));
        headers.add("Authorization", "Bearer " + "アクセストークン");

        // パラメータ設定
        HttpEntity<String> entity = new HttpEntity<String>(headers);

        // URL
        String url;
        url = "https://api.line.me/v2/bot/message/999999999999999/content";

        // リクエスト発行
        ResponseEntity<byte[]> response = restTemplate.exchange(url, HttpMethod.GET, entity, byte[].class, "1");
        
        // 戻り値よりチェック
        if(response.getStatusCode().equals(HttpStatus.OK)) {
            FileOutputStream output = new FileOutputStream(new File("filename.jpg"));
            IOUtils.write(response.getBody(), output);
        }
    }
}

所感

パッケージだけの場合はどのクラスかわかりにくいので動かすのがちょっと手間取ります。まあ、サンプルとして参考にさせていただいているのでわがままはいえませんけどw