如何在 quarkus 中将 String/null 序列化为 json?

问题描述

quarkus 将 String 序列化为纯字符串,null 为空体(http 代码 204)

"foo" -> foo

null ->(空体)

如何使它像 json 一样序列化 String 和 null:

"foo" -> "foo"

空 -> 空

解决方法

你可以尝试这样的事情:

package org.acme.config;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/greeting")
public class GreetingResource {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public String greetirng() {
        return "{\"greeting\": \"Hello,JSON\",\"penalty\": null}";
    }
        
}

检查一下:

$ curl localhost:8080/greeting
{"greeting": "Hello,JSON","penalty": null}