首页 > 解决方案 > 从发布的评论中删除方法名称

问题描述

我编写了一个代码,在我的 localhost:3000 上添加了注释,但它解析了很多我想删除“commentModel”的信息,但是如果我从 CommentRq 类中删除它,我会收到错误

评论示例:{“commentModel”:{“comment”:“komentarz”,“日期”:“3/6/19 9:34 AM”},“id”:1}

我希望它是{“评论”:“komentarz”,“日期”:“3/6/19 9:34 AM”},“id”:1}

评论请求

@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
public class CommentRq {



    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private CommentModel commentModel;
    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    @Builder
    public static class CommentModel {
        @JsonProperty("comment")
        String resourceName;

        @JsonProperty("date")
        String resourceNamed;
    }

}

评论正文

public class CommentBody {

    Date now = new Date();
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public CommentRq RequestCommentBody() {
        return CommentRq.builder()
                .commentModel(new CommentRq.CommentModel(
                        "komentarz",
                        (DateFormat.getInstance().format(now))

                ))
                .build();
    }
}

在这里我创建评论

Interface.PostComment postComment = Feign.builder()
            .client(new OkHttpClient())
            .encoder(new JacksonEncoder())
            .decoder(new JacksonDecoder())
            .logger(new Slf4jLogger(Interface.PostComment.class))
            .logLevel(Logger.Level.FULL)
            .target(Interface.PostComment.class, "http://localhost:3000/comments/");

    @When("i try to add a comment")
    public void postComment() {
        Map<String, Object> headermap = new HashMap<>();
        headermap.put("Content-Type", "application/json");
        CommentBody requestComment = new CommentBody();
        CommentRes commentRes = postComment.postComment(headermap, requestComment.RequestCommentBody());
        id = commentRes.getId();
        LOGGER.info("Created: " + DateFormat.getInstance().format(now));
    }

标签: javalombokfeign

解决方案


你可以private CommentModel commentModel@JsonUnwrapped. 它将打开您的commentModel对象并将其字段写入 json 的根目录。这将处理您的具体情况。但是您也可以修改您的请求结构:将 CommentModel 字段放入 CommentRq 并将 CommentModel 对象映射到 CommentRq 对象。


推荐阅读