0

I want to MAP my HTTP request parameter value directly to my DTO USING @JsonProperty on the basis of the variable name not by @JsonProperty value. I am not able to map the value to DTO because it's expecting request value according to the JsonProperty name. Is there anyway to disable @JsonProperty value while using the @RequestBody ?

JSON send by frontend:

{
"userId":"1",
"payMethod":"payMethod"
}

MyDto.class public class MyDto{

    @JsonProperty(value = user_id, required = true)
    private String userId;


    @JsonProperty(value = BETAALMETHODE, required = true)
    private String payMethod;
//getter setter
   }

MyController.class

public class MyController{

  @RequestMapping(value = "payment", method = RequestMethod.PUT)
    public Integer PaymentUpdate(@RequestBody final MyDto myDto) throws JsonProcessingException {

}
2
  • If you're not going to use JsonProperty why did you define it in the first place? Commented Feb 27, 2020 at 11:04
  • @lealceldeiro I am using this property in one different process where i am uploading data by using the CSV file. Commented Feb 27, 2020 at 11:10

2 Answers 2

0

you can do this by using multiple setter method for that DTO method. For example

Payload:
{
"userId":"1",
"payMethod":"payMethod"
}

then

MyDto.class public class MyDto{

@JsonProperty(value = user_id, required = true)
    private String userId;


    @JsonProperty(value = BETAALMETHODE, required = true)
    private String payMethod;

add one more setter relevant to the required variable name in the DTO class.

@JsonSetter("specifiedName")
void setUserId(String userId){
     this.userId=userId
}

void setPayMethod(String payMethod){ // Will work for "BETAALMETHODE" variable name
     this.payMethod=payMethod
}
@JsonSetter("payMethod")
void setPayMethod(String payMethod){
     this.payMethod=payMethod
}

This will solve your problems and variable payMethod will assign in both the cases.

Sign up to request clarification or add additional context in comments.

1 Comment

Great but you implemented after four month.
0

You can use JacksonMixin during csv parsing:

public abstract class MyDtoMixin {
    @JsonProperty(value = user_id, required = true)
    private String userId;

    @JsonProperty(value = BETAALMETHODE, required = true)
    private String payMethod;
}


ObjectMapper mapper = new ObjectMapper(); // or CsvMapper mapper = new CsvMapper();
mapper.addMixInAnnotations(MyDto.class, MyDtoMixin.class);

1 Comment

Thanks for the suggestion but in this case, I have to create two DTO and I am avoiding this. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.