DEV Community

Masui Masanori
Masui Masanori

Posted on

[Micronaut] Receiving Japanese(Shift-JIS) data as HTTPResponse

Intro

I will try using Japanese(Shift-JIS) text data in my Micronaut application in this time.

Change default file encoding

By default, the encoding for my Micronaut application is "windows-31j".
When I use Japanese in the comment, an exception will occur, so I should change the encoding to UTF-8.

build.gradle.kts

...
tasks.withType<JavaCompile>().configureEach {
    options.encoding = "UTF-8"
}
Enter fullscreen mode Exit fullscreen mode

Setting HTTPResponse encoding

I can use specific encoding for HTTPResponse like below.

HomeController.java

package jp.masanori.responsesample;

import io.micronaut.http.HttpResponse;
import io.micronaut.http.MutableHttpResponse;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;

@Controller("/sample")
public class HomeController {
    @Get(uri="/utf8", produces = "text/plain;charset=utf-8")
    public MutableHttpResponse<String> getUtf8(){        
        return HttpResponse.ok("こんにちは");
    }
    @Get(uri="/shiftjis", produces = "text/plain;charset=Shift_JIS")
    public String getShiftjis(){
        return "こんにちは";
    }
}
Enter fullscreen mode Exit fullscreen mode

Receiving Shift-JIS strings as HTTPResponse by HttpClient

I can receive them by HttpClient like below.

FileController.java

package jp.masanori.websample;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.io.InputStreamReader;
import java.io.ByteArrayInputStream;
import java.io.BufferedReader;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import io.micronaut.core.io.buffer.ByteBuffer;
import io.micronaut.http.HttpHeaders;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.HttpStatus;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Part;
import io.micronaut.http.annotation.Post;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.multipart.CompletedFileUpload;
import jp.masanori.dto.ComputeResult;
import jp.masanori.dto.ComputingStatusRequest;
import reactor.core.publisher.Mono;

@Controller("/files")
public class FileController {
    private final HttpClient httpClient;
    public FileController(HttpClient httpClient) {
        this.httpClient = httpClient;
    }
    @Get(uri="/message", produces = "text/plain;charset=utf-8")
    public Mono<String> getShiftJisText() throws IOException {

        HttpRequest<?> req = HttpRequest.GET("http://localhost:8085/sample/shiftjis");
        return Mono.from(httpClient.retrieve(req));
    }
}
Enter fullscreen mode Exit fullscreen mode

But when I receive a Shift-JIS encoded file, the values will be garbled.

sample.txt

こんにちは
Enter fullscreen mode Exit fullscreen mode

FileController.java

...
    @Get(uri="/message", produces = "text/plain;charset=utf-8")
    public Mono<String> getShiftJisText() throws IOException {

        HttpRequest<?> req = HttpRequest.GET("http://localhost:8085/sample.txt");
        return Mono.from(httpClient.retrieve(req));
    }
...
Enter fullscreen mode Exit fullscreen mode

Result

����ɂ���
Enter fullscreen mode Exit fullscreen mode

To prevent this, I should set the encoding to receive data.

...
    @Get(uri="/message", produces = "text/plain;charset=utf-8")
    public Mono<String> getShiftJisText() throws IOException {
        HttpRequest<?> req = HttpRequest.GET("http://localhost:8085/sample.txt")
            .contentType(MediaType.TEXT_JSON_TYPE);
        // receiving data as ByteBuffer
        HttpResponse<ByteBuffer> res = Mono.from(httpClient.exchange(req, ByteBuffer.class)).block();
        if(res.status() != HttpStatus.OK) {
            return Mono.empty();
        }
        // set encoding and read data
        try(ByteArrayInputStream input = new ByteArrayInputStream(res.body().toByteArray());
            BufferedReader br = new BufferedReader(new InputStreamReader(input, "SJIS"))) {            
            String line = "";
            StringBuilder result = new StringBuilder();
            while((line = br.readLine()) != null) {
                result.append(String.format("%s%s", line, "\r\n"));
            }
            return Mono.just(result.toString());
        }catch(IOException e) {
        }
        return Mono.empty();
    }
...
Enter fullscreen mode Exit fullscreen mode

Result

こんにちは
Enter fullscreen mode Exit fullscreen mode

Top comments (0)