gpt4 book ai didi

java - Spring Boot Web无法将嵌套的json解析为对象

转载 作者:太空宇宙 更新时间:2023-11-04 10:52:53 25 4
gpt4 key购买 nike

首先,我对 spring (web mvc) 很陌生。我正在为我的前端构建 RESTful 服务。我正在发送 JSON 数据,但该数据中的一个对象在 spring 接收时未进行解析。

这是我的数据

JSON 数据

{
"comments" : []
"description": "Testing",
"images": "[\"path1\", \"path2\", \"path3\"]",
"profile": {
"id": 21234,
"fullname": "John Michael Doe",
"nickname": "Doe",
"email": "jdoe@email.com",
"profilePic": "/some/host/pic"
}
}

请求映射器

@RestController
@RequestMapping("/wish")
public class WishlistController extends WishlistConverter{

/* Some @Autowired here for services... */

@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<?> create(@RequestBody RestWishlist input){
try {
logger.info("create({})",input);
Wishlist wish = convert(input);
wishlistService.create(wish);
return new ResponseEntity<>(HttpStatus.OK);
} catch (Exception e) {
logger.error("Error: {}",e.getMessage());
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
}
}
}

休息愿望 list

public class RestWishlist {

private Long wishId;

private List<RestComment> comments;

private String description;

private String images;

private Long createdAt;

private Long updatedAt;

private RestProfile profile;

/* Getters and Setters here */
}

RestProfile

public class RestProfile {

private Long id;

private String fullname;

private String nickname;

private String email;

private String profilePic;
/* Getters and Setters Here */
}

休息评论

public class RestComment {

private Long commentId;

private String description;

private RestProfile profile;

private Long createdAt;

private Long updatedAt;
/* Getters and Setters Here */
}

现在,我在获取 JSON 数据的“配置文件”部分时遇到问题,日志文件显示此内容

2017-12-03 20:50:31.740  INFO 10212 --- [nio-8080-exec-1] c.s.s.web.controller.WishlistController  : create(RestWishlist [wishId=null, comments=[], description=Testing, images=["path1", "path2", "path3"], createAt=null, updatedAt=null])

2017-12-03 20:50:31.740 INFO 10212 --- [nio-8080-exec-1] c.s.s.services.impl.WishlistServiceImpl : create(Wishlist [wishId=null, comments=[], description=Testing, images=["path1", "path2", "path3"], createAt=null, updatedAt=null])

最佳答案

经过几天的跟踪,我发现了问题。这是一个简单的错误,对我来说真的很粗心。

“profile”是根据 json 数据正确设置的。它没有出现在日志中的原因是 RestWishlist 类中的 toString() 方法不包含配置文件。见下文

之前

@Override
public String toString() {
return "RestWishlist [wishId=" + wishId + ", comments=" + comments + ", description=" + description
+ ", images=" + images + ", createdAt=" + createdAt + ", updatedAt=" + updatedAt + "]";
}

之后

@Override
public String toString() {
return "RestWishlist [wishId=" + wishId + ", comments=" + comments + ", description=" + description
+ ", images=" + images + ", createdAt=" + createdAt + ", updatedAt=" + updatedAt + ", profile="
+ profile + "]";
}

我只添加了要在 toString() 方法中显示的配置文件变量

关于java - Spring Boot Web无法将嵌套的json解析为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47618592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com