Reputation: 143
I have an endpoint that gets RequestMessage as a POST body.
I want to map my payload to my java class that uses Lombok with builder pattern and add another variable(myAccountId
) to it that is present in my ParentDto
which my ChildDto extends.
Below is my implementation in method sendMessage but I don't see myAccountId
added in Request Message.
@PostMapping("/sendRequest")
public ResponseEntity<String> sendMessage(@RequestBody RequestMessage payload) {
final RequestMessage reqDto = payload;
reqDto.toBuilder()
.myAccountId(accId)
.build();
publishMesaage(reqDto);
return new ResponseEntity<>(HttpStatus.OK);
}
ChildDTO:
@Getter
@ToString
@SuperBuilder(toBuilder = true)
@EqualsAndHashCode(callSuper = false)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class RequestMessage extends MyDTO {
private final String name;
}
ParentDTO
@Data
@SuperBuilder(toBuilder = true)
public abstract class MyDTO implements Serializable {
@JsonIgnore private final ObjectMapper objectMapper = new ObjectMapper();
protected String myAccountId;
protected MyDTO() {}
public static int hashCode(Object... objects) {
return Arrays.deepHashCode(objects);
}
public static boolean equal(Object o1, Object o2) {
// implementation of equals method
return false;
}
Upvotes: 1
Views: 1351
Reputation: 265595
You are never using your newly built object returned by the build()
method. You need to assign its return value to a reference, so you can use it. Calling methods on the builder does not magically update the original object.
@PostMapping("/sendRequest")
public ResponseEntity<String> sendMessage(@RequestBody RequestMessage payload) {
// 1. get a builder from your payload,
// 2. modify the builder,
// 3. store the new instance returned by `build` in variable
final RequestMessage reqDto = payload.toBuilder()
.myAccountId(accId)
.build();
// 4. pass new instance to your other method
publishMesaage(reqDto);
return new ResponseEntity<>(HttpStatus.OK);
}
Upvotes: 1