当我通过任何字段找到客户时,一切都很顺利,我得到了带有返回的全局对象的 JSON。
我正在编写代码来查找Customer由于他们lastName。客户实体有一个对象,其中的字段lastName被宣布。因此,我希望我的端点像第一种情况一样返回客户全局对象。
我已经进入我的邮递员状态 200 OK,但身体空了。有什么解决办法吗?谢谢
这是一个示例。 LastName 字段在 prefBillAddressUid 和 prefShipAddressUid 对象中声明
@Entity
@Table(name = "tcustomer")
public class Customer implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name = "pk_sequence",sequenceName = "sequence_tcustomer",initialValue = 1000, allocationSize = 100)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "pk_sequence")
private Long uidpk;
@Column(name = "user_id", nullable=false)
private String userId;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "pref_bill_address_uid")
private CustomerAddress prefBillAddressUid;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "pref_ship_address_uid")
private CustomerAddress prefShipAddressUid;
...
//getters and setters
//constructors
}
我的存储库
@Query(value = "SELECT c FROM CustomerAddress c WHERE c.lastName = :lastName")
CustomerAddress findByLastName( @Param("lastName") String lastName);
服务实施
@Override
public CustomerAddressDto findByLastName(String lastName) {
CustomerAddress result = customerRepository.findByLastName(lastName);
return customerAddressMapper.customerAddressToCustomerAddressDto(result);
}
这是我的资源
@GetMapping(SEARCH_CUSTOMER_LAST_NAME_ENDPOINT)
@ResponseStatus(value = HttpStatus.OK)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK", response = CustomerAddressDto.class),
@ApiResponse(code = 500, message = "Unexpected error", response = CustomerAddressDto.class)
})
@Timed
public ResponseEntity getCustomerByLastName ( @PathVariable String lastName) throws URISyntaxException {
if (log.isDebugEnabled()){
log.debug("[CustomerResource] GET {} : Retrieving Customer ({})", SEARCH_CUSTOMER_LAST_NAME_ENDPOINT, lastName);
}
CustomerAddressDto customerAddressDto = customerService.findByLastName(lastName);
return Optional.ofNullable(customerAddressDto)
.map(result->{
if (log.isDebugEnabled()){
log.debug("[CustomerResource] Customer ({}) retrieved", result.getLastName());
}
return new ResponseEntity<>(HttpStatus.OK);
})
.orElse(new ResponseEntity(new ResponseError(HttpStatus.NOT_FOUND.getReasonPhrase(),
"The Customer with lastName " + lastName + " does not exists"), null,HttpStatus.NOT_FOUND)
);
}
客户地址类
@Entity
@Table(name="taddress")
public class CustomerAddress implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name = "pk_sequence",sequenceName = "sequence_taddress",initialValue = 1000, allocationSize = 100)
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "pk_sequence")
private Long uidpk;
@Column(name = "last_name")
private String lastName;
@Column(name = "first_name")
private String firstName;
@Column(name = "phone_number")
private String phoneNumber;
@Column(name= "fax_number")
private String faxNumber;
@Column(name = "street_1")
private String street1;
@Column(name= "street_2")
private String street2;
@Column(name= "city")
private String city;
@Column(name= "sub_country")
private String subCountry;
@Column(name= "zip_postal_code")
private String zipPostalCode;
@Column(name= "country")
private String country;
@Column(name = "commercial")
private Boolean commercial;
@Column(name = "guid", nullable=false)
private String guid;
@Column(name = "customer_uid")
private Long customerUid;
@Column(name= "type")
private String type;
@Column(name = "organization")
private String organization;
...
//getters ans setters
}