我正在嘗試spring-boot與mongodb. 我已經關注了在線文章和教程,并且示例應用程式可以很好地將字串作為 API 回應回傳。但是當我嘗試使用 JSON 回傳回應時ResponseEntity出現錯誤。
這是代碼:
src/customerapi/model/Customer.java
package com.customerapi.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
@Document(collation = "customer")
public class Customer {
@Id
private String custRefId;
private String title;
private String firstName;
private String middleName;
private String lastName;
private String businessName;
private String email;
private String phone;
private String note;
private String dateOfBirth;
private String sex;
}
src/customerapi/dto/CustomerResponse.java
package com.customerapi.dto;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
@JsonInclude(Include.NON_NULL)
public class CustomerResponse {
@JsonProperty("custRefId")
private String custRefId;
}
src/customerapi/repository/CustomerRepository.java
package com.customerapi.repository;
import org.springframework.data.mongodb.repository.MongoRepository;
import com.customerapi.dto.CustomerResponse;
import com.customerapi.model.Customer;
public interface CustomerRepository extends MongoRepository<Customer, String> {
}
src/customerapi/resource/CustomerController.java
package com.customerapi.resource;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import com.customerapi.repository.CustomerRepository;
import com.customerapi.model.Customer;
import com.customerapi.dto.CustomerResponse;
@RestController
public class CustomerController {
@Autowired
private CustomerRepository customerRepository;
@PostMapping(value="/addCustomer", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public ResponseEntity<CustomerResponse> saveCustomer(@RequestBody Customer customer) {
try {
return new ResponseEntity<CustomerResponse>(new customerRepository.save(customer), HttpStatus.CREATED);
}finally {
System.out.println("Issue with saving data in DB");
}
}
}
回傳 a stringfromsaveCustomer()作業正常。但是,當我嘗試回傳ResponseEntity<CustomerResponse>并到達 Postman 中的端點時,出現此錯誤。

我在網上搜索并從 stackoverflow 的答案中嘗試了多種方法,但似乎沒有任何效果對我有用。
**如何將MongoRepository的save回應轉換為自定義類并使用 JSON 作為 JSON 回傳ResponseEntity?**
uj5u.com熱心網友回復:
在這里new CustomerResponse(customerRepository.save(customer))。內部CustomerResponse沒有接受Customer作為引數的構造函式。你可以創建一個。所以你CustomerResponse 會看起來像這樣。
import com.customerapi.model.Customer;
@Getter
@Setter
@ToString
@JsonInclude(Include.NON_NULL)
public class CustomerResponse {
@JsonProperty("custRefId")
private String custRefId;
public CustomerResponse(Customer customer){
this.custRefId = customer.getCustRefId();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/310819.html
上一篇:截斷鏈表的遞回解決方案
