0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Jacksonでjson文字列生成

Posted at
package com.example.todo.api.todo;

import java.util.LinkedHashMap;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtil {

    public static String createErrorMessageJson(String messageId, String message) {
        try {
            Map<String, String> map = new LinkedHashMap<>();
            map.put("messageId", messageId);
            map.put("message", message);
            ObjectMapper mapper = new ObjectMapper();
            return mapper.writeValueAsString(map);
        } catch (Exception e) {
            throw new RuntimeException("JSON生成失敗", e);
        }
    }
}
String json = JsonUtil.createErrorMessageJson("E002", "無効な入力です");
System.out.println(json);

実行例

{"messageId":"E002","message":"無効な入力です"}
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?