我正在使用Postman进入一个JSON POST到本地主机,我有一个Springboot应用程序在运行。
POST是:
{"applicantName":"On Gaber","amount":"5000","asset":"Car","creditScore":"602"}
控制器@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
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);
}
库类:
public interface LoanApplicationRepository extends
MongoRepository<LoanApplication, String> {
@SuppressWarnings("unchecked")
LoanApplication save(LoanApplication saved);
DTO类:
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;
}
}
域类:
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会自动连接并配置整个应用程序。不需要web.xml或Spring配置,Springboot会完成这一切。
application.properties:
logging.level.org.springframework.web=DEBUG
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
JSON响应:
{
"id": "55c39c23f04a07251bc598b0",
"applicantName": "On Gaber",
"amount": "5000",
"asset": null,
"creditScore": null
}
简略Eclipse控制台输出:
: Started LoanApplicationProcessor in 5.258 seconds (JVM running for 6.012)
: Successfully completed request
domain和DTO中的所有字段都是String类型。我不明白为什么"asset"one_answers"creditscore"字段是空的。什么好主意吗?
您没有将这些字段的值分配给public LoanApplicationDTO create(LoanApplicationDTO loanApplicationDTO)
方法中的新实体,因此这些值不会被持久化。