找不到类 org.jooq.JSON 的序列化程序,也没有发现用于创建 BeanSerializer 的属性

发布于 2025-01-16 04:34:49 字数 878 浏览 1 评论 0原文

通过将方法实现为 API 服务,我遇到了这个问题

找不到类 org.jooq.JSON 的序列化程序,也没有属性 发现创建 BeanSerializer (为了避免异常,禁用 SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过引用链: java.util.ArrayList[0]-> db.jooq.tables.pojos.Query["queryJson"])

我的查询(表)的 Jooq POJO 是:

 private Integer id; 
 private JSON    queryJson;

给出查询列表作为响应的方法是

 @Produces(MediaType.APPLICATION_JSON)   public Response
 getQueries(@PathParam("ownerId") String ownerId,
       @PathParam("roomId") String hotelroom) throws JsonProcessingException {
     List<Query> queries = DatabaseUtil.getQueries(dsl, UUID.fromString(hotelroom));
     return Response.ok(queries).build();}

但是没有序列化,如果我更改 jooq POJO 对此:

private Integer id; 
private String queryJson;

那么我的响应的输出将不会通过序列化而出现错误。 有谁知道如何在 jooq 中解决这个问题吗?

By implementing a method as a service for API, I faced this problem

No serializer found for class org.jooq.JSON and no properties
discovered to create BeanSerializer (to avoid exception, disable
SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain:
java.util.ArrayList[0]-> db.jooq.tables.pojos.Query["queryJson"])

The Jooq POJO of my Query (TABLE) is :

 private Integer id; 
 private JSON    queryJson;

and the method which give a list of query as a respond is

 @Produces(MediaType.APPLICATION_JSON)   public Response
 getQueries(@PathParam("ownerId") String ownerId,
       @PathParam("roomId") String hotelroom) throws JsonProcessingException {
     List<Query> queries = DatabaseUtil.getQueries(dsl, UUID.fromString(hotelroom));
     return Response.ok(queries).build();}

But there is no serialize and if I change the jooq POJO to this :

private Integer id; 
private String queryJson;

Then the output of my response will have no error by serializing.
Does any one have an idea, how to solve this problem in jooq?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过期以后 2025-01-23 04:34:49

假设您使用的是 Jackson,您可以使用 JsonRawValue< 注释您的 JSON 属性/a>:

private Integer id; 
@JsonRawValue
private JSON    queryJson;

如果您还需要从 JSON 读取值,您可以这样做:

private Integer id; 
@JsonRawValue
@JsonDeserialize(using = JSONDeserializer.class)
private JSON    queryJson;

... with:

class JSONDeserializer extends JsonDeserializer<JSON> {

    @Override
    public JSON deserialize(JsonParser p, DeserializationContext ctxt)
    throws IOException {
        Object t = p.readValueAsTree();
        return t == null ? null : JSON.json("" + t);
    }
}

未来的 jOOQ 版本(尚未到 3.16)可能会在代码生成器中为此类注释提供本机支持: https://github.com/jOOQ/jOOQ/issues/13333

Assuming you're using Jackson, you can annotate your JSON property with JsonRawValue:

private Integer id; 
@JsonRawValue
private JSON    queryJson;

If you need to also read the value from JSON, you can do this:

private Integer id; 
@JsonRawValue
@JsonDeserialize(using = JSONDeserializer.class)
private JSON    queryJson;

... with:

class JSONDeserializer extends JsonDeserializer<JSON> {

    @Override
    public JSON deserialize(JsonParser p, DeserializationContext ctxt)
    throws IOException {
        Object t = p.readValueAsTree();
        return t == null ? null : JSON.json("" + t);
    }
}

A future jOOQ version (not yet 3.16), might offer native support in the code generator for such annotations: https://github.com/jOOQ/jOOQ/issues/13333

倾城花音 2025-01-23 04:34:49

您无法在生成的 JOOQ 源中注释字段,因为自定义注释将在下一次代码生成期间被覆盖。

在寻找答案时,我发现 这个博客文章

总而言之,您需要创建一个自定义序列化程序,使 Jackson 能够了解如何序列化 org.jooq.JSON 对象。

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import org.jooq.JSON;

import java.io.IOException;

public class JooqJsonSerializer extends StdSerializer<JSON> {
    public JooqJsonSerializer() {
        super(JSON.class);
    }

    @Override
    public void serialize(JSON value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        gen.writeRawValue(value.data());
    }
}

然后通过在您应该已有的任何 Spring @Configuration 类中添加配置方法来配置 Jackson 以使用它。

@Bean
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
    return builder ->
            builder.serializationInclusion(JsonInclude.Include.USE_DEFAULTS)
                    .serializers(new JooqJsonSerializer());
}

You cannot annotate fields in the generated JOOQ sources because the custom annotation will be overriden during the next code generation.

While looking for the answer, I found this blog post.

To sum up, you need to create a custom serializer that will allow Jackson to understand how to serialize the org.jooq.JSON objects.

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import org.jooq.JSON;

import java.io.IOException;

public class JooqJsonSerializer extends StdSerializer<JSON> {
    public JooqJsonSerializer() {
        super(JSON.class);
    }

    @Override
    public void serialize(JSON value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        gen.writeRawValue(value.data());
    }
}

Then configure the Jackson to use it by adding the configuration method in any of the Spring @Configuration classes you should already have.

@Bean
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
    return builder ->
            builder.serializationInclusion(JsonInclude.Include.USE_DEFAULTS)
                    .serializers(new JooqJsonSerializer());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文