java - 从发布的评论中删除方法名称
问题描述
我编写了一个代码,在我的 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));
}
解决方案
你可以private CommentModel commentModel
用 @JsonUnwrapped
. 它将打开您的commentModel
对象并将其字段写入 json 的根目录。这将处理您的具体情况。但是您也可以修改您的请求结构:将 CommentModel 字段放入 CommentRq 并将 CommentModel 对象映射到 CommentRq 对象。
推荐阅读
- python - Django 从查询集中获取查询。返回无效参数
- jestjs - 如何配置 jest 来处理 SvelteKit 的 `$lib` 别名?
- spring-cloud - 如何将 DefaultMessageListenerContainer 与 Spring 的 SqsListener 一起使用?
- mule-studio - Scatter-Gather 中的错误处理 (Mule 4)
- git - 为什么使用高延迟网络时 git 很慢
- android - Jetpack Compose Preview 使用 ThreeTen 的 LocalDate.now() 失败
- c++ - 使用柯南时需要上网吗?
- java - java中批量运算的计算器
- django - 如何处理 Django API 框架的 .errors
- c++ - 我无法正确访问“图形”类的功能