Spring Boot web无法将嵌套的json解析为对象

时间:2017-12-03 12:57:03

标签: java spring spring-mvc spring-boot jackson

首先,我对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"
    }
}

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

RestWishlist

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

RestComment

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])

1 个答案:

答案 0 :(得分:0)

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

&#34;轮廓&#34;是从json数据正确设置的。它未出现在日志中的原因是因为toString()类中的RestWishlist方法不包含该配置文件。见下文

<强>之前

@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()方法

中显示的配置文件变量