-1

I made a code which add comments on my localhost:3000 but its parsing to much info i want to remove "commentModel" but if i remove it from CommentRq class i get errors

comment example: { "commentModel": { "comment": "komentarz", "date": "3/6/19 9:34 AM" }, "id": 1}

i want it to be { "comment": "komentarz", "date": "3/6/19 9:34 AM" }, "id": 1 }

CommentRq

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

}

CommentBody

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

Here i create comment

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));
    }
p0l0
  • 11
  • 4
  • Maybe tell us which *errors* you're getting? – Lino Mar 06 '19 at 10:57
  • Cannot resolve method 'commentModel(testyLocalhost.CommentRq.CommentModel)' when i remove private CommentModel commentModel; from CommentRq – p0l0 Mar 06 '19 at 11:00

1 Answers1

1

You can annotate your private CommentModel commentModel with @JsonUnwrapped. It will unwrap your commentModel object and write its fields to the root of the json. This will handle your specific case. But you can revise your request structure as well: put CommentModel fields into CommentRq and map CommentModel object to CommentRq object.

  • thx for reply but @JsonUnwrapped isnt working and im not sure how to do second thing you mentioned can you give me the code? – p0l0 Mar 06 '19 at 11:50
  • It should work. Please, remove `@JsonProperty` annotation and add `@JsonUnwrapped`. – Mykhailo Hodovaniuk Mar 06 '19 at 12:10
  • yes its working i needed to add it in other place aswell – p0l0 Mar 06 '19 at 12:11
  • The second option is to do not embed `CommentModel` object and have `CommentRq` like this ```public class CommentRq { @JsonProperty("comment") String resourceName; @JsonProperty("date") String resourceNamed; }``` – Mykhailo Hodovaniuk Mar 06 '19 at 12:14