2

first of all, i am quite new to spring (web mvc). I am building a RESTful service for my frontend. I am sending a JSON data but one object in that data was not parsed when it is received by spring.

Here is my data

JSON Data

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

Now, i have a problem getting the "profile" part of my JSON data, log file show this

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])
lemoncodes
  • 2,371
  • 11
  • 40
  • 66
  • While sending data from service profile object is not received is it ? – swapnil Dec 03 '17 at 13:07
  • If value of profile attribute is null then it might get skipped from the resulting JSON. – swapnil Dec 03 '17 at 13:07
  • @swapnil - based from my JSON data i sent to spring, "profile" has value, please see my JSON data. – lemoncodes Dec 03 '17 at 13:09
  • Try '@ModelAttribute' instead of '@RequestBody'. – swapnil Dec 03 '17 at 13:18
  • tried using @ModelAttribute but all fields in my JSON data became null after spring received it. – lemoncodes Dec 03 '17 at 13:26
  • I dsont see "profile" in controller log are you using @JsonIgnoreProperties(ignoreUnknown = true) thats why ? because it should come as null at-least or Model is not in sync ? To me it looks like either it is issue with Jackson conversion or ex: @JsonProperty("property_name") or your model is not in sync – Om. Dec 03 '17 at 13:51
  • Yep that is the problem, “profile” does not get parsed or converted from JSON data i sent. I did not use any annotations for that. I just passed it as is from and received it as is. Please look at the wishlistcontroller log. That is the part when rest recieves the json data – lemoncodes Dec 03 '17 at 14:00
  • Did you try @JsonManagedReference annotation? Ref - https://stackoverflow.com/questions/37392733/difference-between-jsonignore-and-jsonbackreference-jsonmanagedreference/37394318 – vsoni Dec 07 '17 at 15:17

1 Answers1

0

So i've found the problem after days of tracing. it's a simple bug and really, for me, careless.

"profile" was correctly set from json data. the reason why it does not appear in the logs is because the toString() method in RestWishlist class does not contain the profile. See below

Before

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

After

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

I only added the profile variable to be displayed in toString() method

lemoncodes
  • 2,371
  • 11
  • 40
  • 66