Jackson

Jacksonを使って、Java Objectをjsonに変換する場合のコードです。Java Objectはpublicのフィールドがあればgetterはなくてもよいです。

ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(bean);

jsonには、beanのプロパティ名をキーとするイメージが出力されます。プロパティ名と異なるキーにする場合には、@JsonPropertyを使用することができます。

public class Bean {

    @JsonProperty("user_id")
    public Integer id;

    public String name;
    
}

このBeanクラスからjsonに変換したイメージは下のようになります。

{"user_id":null,"name":null}

特定のクラスや特定のフィールドのnull値の項目のキーが生成されないようにしたい場合は、クラスまたはフィールドにアノテーションを記述します。

@JsonInclude(JsonInclude.Include.NON_NULL)
public class Bean {
@JsonInclude(JsonInclude.Include.NON_NULL)
public String name;

または、ObjectMapperを設定してnull値の項目のキーが生成されないようにできます。

mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
String json = mapper.writeValueAsString(bean);

Spring Bootアプリケーション

ObjectMapperにInclude.NON_NULLを設定したい場合は、application.propertiesに記述します。

spring.jackson.serializationInclusion=NON_NULL

Gradleでのプロパティファイルの拡張(expand)

Spring Bootアプリケーションで、検証環境と本番環境とでプロパティの値を変更するためのビルドスクリプトを書いてみました。

GradleのドキュメントSpring Bootのリファレンスを参考にしています。

ここでは、例としてspring.profiles.activeの値を設定してみます。application.propertiesには次のように書きます。

spring.profiles.active=${ext.profiles}

次に、build.gradleにprocessResourcesを記述します。

processResources {
    filesMatching('**/application.properties') {
        expand(project.properties)
    }
}

-Pprofiles=fooを指定してbuildを実行します。 build/resourcesにコピーされたapplication.propertiesは下のようになります。

spring.profiles.active=foo

置換してほしくないプロパティについては、$の前に(バックスラッシュ)を付加します。

spring.datasource.url=\${url}

build/resourcesにコピーされたプロパティファイルは下のようになります。

spring.datasource.url=${url}

Azure IoT Hubにデバイスを登録する

Azure IoT Hubにデバイスを登録する時、WindowsだとDevice Explorerを使用します、Windows以外の場合はiothub-explorerを使用します。

ここでは、LinuxMac OS Xのような非Windowsを対象とします。

前提

  • Node.JSとnpmがインストールされていることが必要です。

iothub-explorerのインストール

$ sudo npm install -g iothub-explorer@latest
$ iothub-explorer help
  • Azure PortalのIoT Hubの設定から、共有アクセスポリシーを選択し、iothubownerの接続文字列をクリップボードにコピーして、つぎのコマンドを実行します。
$ iothub-explorer login "HostName=<my-hub>.azure-devices.net;SharedAccessKeyName=<my-policy>;SharedAccessKey=<my-policy-key>"
  • new-device-idの部分に登録するデバイスIDを設定します。
$ iothub-explorer create <new-device-id> --connection-string

Angularのui-bootstrapでmenuitemをdisabledにする

Angular 1.5のui-bootstrapでDropdown MenuのMenuItemをdisabledにするいい方法が今ひとつ見つからず、 ng-classを使ってdisabledにするようにしました。

Version

  • angular - 1.5.0
  • bootstrap - 3.3.6
<div uib-dropdown>
    <a href id="simple-dropdown" uib-dropdown-toggle> Click me </a>
    <ul uib-dropdown-menu aria-labelledby="simple-dropdown">
        <li role="menuitem" ng-class="{'disabled': one}"><a href>one</a>
        </li>
        <li role="menuitem" ng-class="{'disabled': two}"><a href>two</a>
        </li>
        <li role="menuitem" ng-class="{'disabled': three}"><a href>three</a>
        </li>
    </ul>
</div>

https://github.com/takesection/angular-dropdown-menu

Eclipseでdoma2を使うためのGradleの設定

Version

  • Spring Boot - 1.3.2.RELEASE
  • doma-spring-boot - 1.0.1

Gradleのビルドスクリプト

EclipseのAPT処理を有効にして、doma2によるAPT処理ができるようにbuild.gradleで設定します。

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.2.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'spring-boot'
apply plugin: 'eclipse'

processResources.destinationDir = compileJava.destinationDir
compileJava.dependsOn processResources
compileJava.options.compilerArgs = ['-Adoma.dao.subpackage=impl', '-Adoma.dao.suffix=Impl']

sourceCompatibility = '1.8'
targetCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

eclipse {
    jdt {
        file {
            withProperties {properties ->
                properties.setProperty('org.eclipse.jdt.core.compiler.processAnnotations', 'enabled')
            }
        }
    }
}

eclipseJdt {
    doFirst {
        def factorypath = file('.factorypath')
        def writer = new FileWriter(factorypath)
        def xml = new groovy.xml.MarkupBuilder(writer)
        xml.setDoubleQuotes(true)
        xml.'factorypath'() {
            def domaJar = configurations.compile.find {
                it.name.startsWith('doma-2')
            }
            'factorypathentry'(kind: 'EXTJAR', id: domaJar, enabled: true, runInBatchMode: false)
        }
    }
}

repositories {
    mavenCentral()
}

configurations.all {
    resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
    compile 'org.seasar.doma.boot:doma-spring-boot-starter:1.0.1'

    compile 'org.xerial:sqlite-jdbc:3.8.11.2'
    
    testCompile 'junit:junit'
    testCompile 'org.springframework.boot:spring-boot-starter-test'
}

https://github.com/takesection/spring-boot-doma2-eclipse

Spring BootアプリケーションでDB2を使用する設定

Version

  • Spring Boot - 1.3.2.RELEASE
  • doma-spring-boot - 1.0.1

Application Properties

doma.dialect=DB2

spring.datasource.url=jdbc:db2://[HOST]:[PORT]/[DATABASE]:currentSchema=[SCHEMA];
spring.datasource.driver-class-name=com.ibm.db2.jcc.DB2Driver
spring.datasource.username=[USERNAME]
spring.datasource.password=[PASSWORD]

https://github.com/takesection/spring-boot-db2