3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaBeanの内容を文字列に出力する

Last updated at Posted at 2016-12-01

Apache commons-langのReflectionToStringBuilderを使ってJavaBeanの内容を文字列に出力する。

検証対象JavaBean

2016-12-01_132739.jpg

サンプルコード

import org.apache.commons.lang3.builder.RecursiveToStringStyle;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;

public class SampleClass {
	
	public String convert2String(Object object) {
		
		// JavaBeanの内容を文字列に出力する
		return new ReflectionToStringBuilder(object, new RecursiveToStringStyle()).toString();
	}
}

出力結果

ProductInfo@be858[productName=iPhone,pcs=100,productionDate=java.util.Date@1476842[],productDetailInfo=ProductDetailInfo@1c170f0[version=7.0,review=<null>,stock=true],storeList=java.util.ArrayList@ed5528{StoreInfo@26b418[storeInfo={StoreName=山田電気, tel=03012349876, adress=東京都新宿区}],StoreInfo@7e868[storeInfo={StoreName=ヨドバシ, tel=03043215678, adress=東京都中央区}]}]

ライブラリ

commons-lang3-3.3.jar

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.3.2</version>
</dependency>
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?