Evgenii Olenskii
Evgenii Olenskii

Reputation: 41

Spring @RequestBody from interface

I have classes generated from .raml file. In the generated interface for controller I have @RequestBody on my parameter. If I try to make request, mapping works correct but every time I have null fields in my Object annotated with @RequestBody from parameters. Looks like this annotation is ignored. How can I make it work from interface.

For testing without Raml I tried to create a simple interface for controller with simple implementation and I'm still getting null fields values in my request object.

Controller interface generated from .raml

@RestController
@RequestMapping("/kbm")
public interface KbmController {

    @RequestMapping(value = "", method = RequestMethod.PUT)
    public ResponseEntity<KbmCalcResponse> updateKbm(
        @Valid
        @RequestBody
        KbmCalcRequest kbmCalcRequest);
}

My implementation

@Component
@RequiredArgsConstructor
public class CalcKbmControllerImpl implements KbmController {

  private final KbmService kbmService;

  @Override
  public ResponseEntity<KbmCalcResponse> updateKbm(KbmCalcRequest kbmCalcRequest) {
    System.out.println(kbmCalcRequest.getInsurerID());
    return ResponseEntity.ok(kbmService.calculate(kbmCalcRequest));
  }
}

Request model generated from .raml

public class KbmCalcRequest implements Serializable
{

    final static long serialVersionUID = 1692733266431420440L;

    private String insurerID;

    public KbmCalcRequest() {
        super();
    }


    public KbmCalcRequest(String insurerID {
        super();
        this.insurerID = insurerID;
    }

    public String getInsurerID() {
        return insurerID;
    }

    public void setInsurerID(String insurerID) {
        this.insurerID = insurerID;
    }

    public int hashCode() {
        return new HashCodeBuilder().append(insurerID).toHashCode();
    }

    public boolean equals(Object other) {
        if (other == null) {
            return false;
        }
        if (other == this) {
            return true;
        }
        if (this.getClass()!= other.getClass()) {
            return false;
        }
        KbmCalcRequest otherObject = ((KbmCalcRequest) other);
        return new EqualsBuilder().append(insurerID, otherObject.insurerID).isEquals();
    }

    public String toString() {
        return new ToStringBuilder(this).append("insurerID", insurerID).toString();
    }

}

Upvotes: 0

Views: 1017

Answers (1)

Evgenii Olenskii
Evgenii Olenskii

Reputation: 41

The problem was in a spring boot starter. We have used an old version of the spring-boot-starter-parent - 2.0.1.RELEASE which took the spring web 4.3.5. But the feature of inheriting annotations from method's params to the implementations of these methods were added at 5.1.0.RELEASE of spring web. So, I've just put the latest version (2.1.5.RELEASE for now) in my pom file and it solved my problem.

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.5.RELEASE</version>
    <relativePath/>
</parent>

Upvotes: 1

Related Questions