0

我正在从 postgresql 数据库获取信息的 webflux 服务中工作。当我从 Intellij Idea 运行它时,该服务运行良好。但是,当我生成本机图像时,一些响应数据是空的(我认为这是由于杰克逊的一些错误配置,但我无法修复它)。有人可以帮忙吗?

这是我拥有的存储库:

@Repository
public interface BookAuthorsRepository extends ReactiveCrudRepository<BookAuthors, Long> {

    @Query("SELECT b.id, b.title, b.description, " +
            "( SELECT json_agg(json_build_object('authorId', a.id, 'fullName', concat_ws(' ', a.name, a.surname))) " +
            "  FROM authors a JOIN books_authors ba ON a.id=ba.author_id " +
            "  WHERE b.id=ba.book_id ) as authors" +
            "  FROM books b")
    Flux<BookAuthors> findAll();

    @Query("SELECT b.id, b.title, b.description, " +
            "( SELECT json_agg(json_build_object('authorId', a.id, 'fullName', concat_ws(' ', a.name, a.surname))) " +
            "  FROM authors a JOIN books_authors ba ON a.id=ba.author_id " +
            "  WHERE b.id=ba.book_id ) as authors" +
            "  FROM books b " +
            "  WHERE b.id=:bookId" )
    Mono<BookAuthors> findByBookId(final Long bookId);
}

我还有一个转换器,它基本上返回 BookAuthors 对象:

public class BookAuthorsReadConverter implements Converter<Row, BookAuthors> {
    private static Logger LOG = LoggerFactory.getLogger(BookAuthorsReadConverter.class);

    @Override
    public BookAuthors convert(Row row) {
        final Long id = row.get("id", Long.class);
        final String bookTitle = row.get("title", String.class);
        final String bookDescription = row.get("description", String.class);

        final List<AuthorRef> authors = new java.util.ArrayList<>();

        final JSONArray array = new JSONArray(row.get("authors", String.class));
        array.forEach(item -> {
                    final JSONObject jsonObject = (JSONObject) item;
                    final AuthorRef authorRef = new AuthorRef(Long.parseLong(jsonObject.get("authorId").toString()),
                            jsonObject.get("fullName").toString());
                    authors.add(authorRef);
                }
        );

        LOG.info("Book with authors: {}", new BookAuthors(id, bookTitle, bookDescription, authors));
        return new BookAuthors(id, bookTitle, bookDescription, authors);
    }
}

在回复中,我得到了这个:

{
    "id": 2,
    "title": "Hands-On Spring Security 5 for Reactive Applications",
    "description": "Learn effective ways to secure your applications with Spring and Spring WebFlux (English Edition)",
    "authors": [
        {}
    ]
}

当我应该得到这个时:

{
    "id": 2,
    "title": "Hands-On Spring Security 5 for Reactive Applications",
    "description": "Learn effective ways to secure your applications with Spring and Spring WebFlux (English Edition)",
    "authors": [
        {
            "authorId": 2,
            "fullName": "Tomcy John"
        }
    ]
}
```

Do anyone know what's going on in the native image? What am I missing? Please help.

Thank you very much in advance.
Best regards.
4

0 回答 0