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":"無効な入力です"}