Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringBootWeb无法将嵌套的json解析为对象_Java_Spring_Spring Mvc_Spring Boot_Jackson - Fatal编程技术网

Java SpringBootWeb无法将嵌套的json解析为对象

Java SpringBootWeb无法将嵌套的json解析为对象,java,spring,spring-mvc,spring-boot,jackson,Java,Spring,Spring Mvc,Spring Boot,Jackson,首先,我对spring(WebMVC)很陌生。我正在为我的前端构建一个RESTful服务。我正在发送一个JSON数据,但当spring接收到该数据时,该数据中的一个对象未被解析 这是我的数据 JSON数据 { "comments" : [] "description": "Testing", "images": "[\"path1\", \"path2\", \"path3\"]", "profile": { "id": 21234,

首先,我对spring(WebMVC)很陌生。我正在为我的前端构建一个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"
    }
}
RequestMapper

@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);
            }
        }
}
@Override
public String toString() {
    return "RestWishlist [wishId=" + wishId + ", comments=" + comments + ", description=" + description
            + ", images=" + images + ", createdAt=" + createdAt + ", updatedAt=" + updatedAt + ", profile="
            + profile + "]";
}
RestComment

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 */
}
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])

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

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

之前

public class RestProfile {

    private Long id;

    private String fullname;

    private String nickname;

    private String email;

    private String profilePic;
    /* Getters and Setters Here */
}
@Override
public String toString() {
    return "RestWishlist [wishId=" + wishId + ", comments=" + comments + ", description=" + description
            + ", images=" + images + ", createdAt=" + createdAt + ", updatedAt=" + updatedAt + "]";
}
之后

@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);
            }
        }
}
@Override
public String toString() {
    return "RestWishlist [wishId=" + wishId + ", comments=" + comments + ", description=" + description
            + ", images=" + images + ", createdAt=" + createdAt + ", updatedAt=" + updatedAt + ", profile="
            + profile + "]";
}

我只添加了要在
toString()
method

中显示的配置文件变量,而没有从服务配置文件对象发送数据,是吗?如果配置文件属性的值为null,则可能会从结果JSON中跳过它。@swapnil-根据我发送给spring的JSON数据,“profile”有值,请查看我的JSON数据。尝试使用“@modeldattribute”而不是“@RequestBody”。尝试使用@modeldattribute,但在spring接收到JSON数据后,JSON数据中的所有字段都变为空。若要解析嵌套的JSON,是否不需要创建反序列化器方法来转换为Java POJO?