简体   繁体   中英

REST POST to Java app returns JSON response with some fields null

I am entering a JSON POST using Postman to localhost where I have a Springboot app running.

The POST is:

{"applicantName":"On Gaber","amount":"5000","asset":"Car","creditScore":"602"}

Controller

@RestController
public class LoanSubmissionController {
private LoanService loanService;
@Autowired
public LoanSubmissionController(LoanService loanService) {
    this.loanService = loanService;
}

@RequestMapping(method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
LoanApplicationDTO create(
        @RequestBody @Valid LoanApplicationDTO loanApplicationDTO) {
    return loanService.create(loanApplicationDTO);

}

Service Class:

@Service
public class LoanSubmissionService implements LoanService {

@Autowired
private final LoanApplicationRepository repository;

@Autowired
public LoanSubmissionService(LoanApplicationRepository repository) {
    this.repository = repository;
}

// @Override
public LoanApplicationDTO create(LoanApplicationDTO loanApplicationDTO) {
    LoanApplication persisted = LoanApplication.getBuilder()
            .applicantName(loanApplicationDTO.getApplicantName())
            .amount(loanApplicationDTO.getAmount()).build();
    persisted = repository.save(persisted);
    return convertToDTO(persisted);
}

Repository Class:

public interface LoanApplicationRepository extends
    MongoRepository<LoanApplication, String> {

@SuppressWarnings("unchecked")
LoanApplication save(LoanApplication saved);

DTO Class:

package com.liquidlogic.loanprocessor.web;

public final class LoanApplicationDTO {

private String id;
private String applicantName;
private String amount;
private String asset;
private String creditScore;

public LoanApplicationDTO() {
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getApplicantName() {
    return applicantName;
}

public void setApplicantName(String applicantName) {
    this.applicantName = applicantName;
}

public String getAmount() {
    return amount;
}

public void setAmount(String amount) {
    this.amount = amount;
}

public String getAsset() {
    return asset;
}

public void setAsset(String asset) {
    this.asset = asset;
}

public String getCreditScore() {
    return creditScore;
}

public void setCreditScore(String creditScore) {
    this.creditScore = creditScore;
}

}

Domain Class:

import org.springframework.data.annotation.Id;
import org.springframework.util.Assert;

public class LoanApplication implements Application {

@Id
private String id;
private String applicantName;
private String amount;
private String asset;
private String creditScore;

public LoanApplication() {
}

private LoanApplication(ApplicationBuilder builder) {
    Assert.notNull(builder);
    this.applicantName = builder.applicantName;
    this.amount = builder.amount;
    this.asset = builder.asset;
    this.creditScore = builder.creditScore;
}

public static ApplicationBuilder getBuilder() {
    return new ApplicationBuilder();
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getAmount() {
    return amount;
}

public void setAmount(String amount) {
    this.amount = amount;
}

public String getAsset() {
    return asset;
}

public void setAsset(String asset) {
    this.asset = asset;
}

public String getCreditScore() {
    return creditScore;
}

public void setCreditScore(String creditScore) {
    this.creditScore = creditScore;
}

public void setApplicantName(String applicantName) {
    this.applicantName = applicantName;
}

public String getApplicantName() {
    return applicantName;
}

@Override
public String toString() {
    return "LoanApplication [id=" + id + ", applicantName=" + applicantName
            + ", amount=" + amount + ", asset=" + asset + ", creditScore="
            + creditScore + "]";
}

public void update(String applicantName, String amount, String asset,
        String creditScore) {
    this.applicantName = applicantName;
    this.amount = amount;
    this.asset = asset;
    this.creditScore = creditScore;
}

public static class ApplicationBuilder {

    private String applicantName;
    private String amount;
    private String asset;
    private String creditScore;

    private ApplicationBuilder() {
    }

    public ApplicationBuilder applicantName(String applicantName) {
        this.applicantName = applicantName;
        return this;
    }

    public ApplicationBuilder amount(String amount) {
        this.amount = amount;
        return this;
    }

    public ApplicationBuilder asset(String asset) {
        this.asset = asset;
        return this;
    }

    public ApplicationBuilder creditScore(String creditScore) {
        this.creditScore = creditScore;
        return this;
    }

    public LoanApplication build() {
        LoanApplication build = new LoanApplication(this);
        return build;
    }

}

}

Springboot autowires up and configures the entire app. There's no need for a web.xml or Spring configuration, Springboot does it all.

application.properties:

logging.level.org.springframework.web=DEBUG
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017

JSON Response:

{
  "id": "55c39c23f04a07251bc598b0",
  "applicantName": "On Gaber",
  "amount": "5000",
  "asset": null,
  "creditScore": null
}

Abbreviated Eclipse Console output:

: Started LoanApplicationProcessor in 5.258 seconds (JVM running for 6.012)
: Successfully completed request

All fields in the domain and DTO are type String. I can't figure out why the "asset" and "credtiScore" fields are null. Any ideas?

您不会在public LoanApplicationDTO create(LoanApplicationDTO loanApplicationDTO)方法中将这些字段的值分配给新实体,因此不会public LoanApplicationDTO create(LoanApplicationDTO loanApplicationDTO)这些值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM