从发布的评论中删除方法名称

时间:2019-03-06 10:44:28

标签: java lombok feign

我做了一个代码,在我的本地主机上添加了注释:3000,但是它解析了很多信息,所以我想删除“ commentModel”,但是如果我从CommentRq类中删除它,则会出错

注释示例: {“ commentModel”:{“ comment”:“ komentarz”,“ date”:“ 3/6/19 9:34 AM”},“ id”:1}

我希望它为{“ comment”:“ komentarz”,“ date”:“ 3/6/19 9:34 AM”},“ id”:1}

CommentRq

@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;
    }

}

CommentBody

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));
    }

1 个答案:

答案 0 :(得分:1)

您可以用private CommentModel commentModel注释@JsonUnwrapped。它将解开commentModel对象,并将其字段写入json的根。这将处理您的具体情况。但是您也可以修改请求结构:将CommentModel字段放入CommentRq,然后将CommentModel对象映射到CommentRq对象。