Already created Rest Api in Spring Boot function which create new order with few variables and also with picture as a Blob and send it to the database.
And i it all works as it should, but right now when I'm trying get this picture as a response from database I'm getting this Blob as a null.
My Controller code is as shown below.
@GetMapping(path = "/getallorders")
public List<OrderAllUsersResponse> getAllOrders() {
List<OrderAllUsersResponse> returnValue = new ArrayList<>();
List<OrderEntity> orders = orderRepostiory.findAllOrders();
ModelMapper modelMapper = new ModelMapper();
for (int i=0; i < orders.size(); i ) {
returnValue.add(modelMapper.map(orders.get(i), OrderAllUsersResponse.class));
}
return returnValue;
}
My Repository code is as shown below.
public interface OrderRepostiory extends JpaRepository<OrderEntity, Long> {
@Query(value = "SELECT * FROM 34671478_opionion.cargo", nativeQuery = true)
List<OrderEntity> findAllOrders();
}
@Entity(name = "cargo")
public class OrderEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(nullable = false)
private String orderId;
@Column(nullable = false)
private String customer;
@Column(nullable = false)
private String loadingCompanyName;
@Column(nullable = false)
private String loadingCity;
@Column(nullable = false)
private String loadingPostcode;
@Column(nullable = false)
private String dateOfLoading;
@Column(nullable = false)
private String unloadingCompanyName;
@Column(nullable = false)
private String unloadingCity;
@Column(nullable = false)
private String unloadingPostcode;
@Column(nullable = false)
private String dateOfUnloading;
@Column(nullable = false)
private Double nettoPrice;
@Column(nullable = false)
private Double bruttoPrice;
@Column(nullable = false)
private String information;
@Column(nullable = false)
private Boolean paymentStatus = false;
@ManyToOne
@NotFound(action= NotFoundAction.IGNORE)
@JoinColumn(name = "company_id")
private CompanyEntity companyDetails;
@Lob
@Column(name = "photo", columnDefinition="BLOB")
private byte[] data;
public byte[] getData() {
return data;
}
public void setData(byte[] data) {
this.data = data;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getCustomer() {
return customer;
}
public void setCustomer(String customer) {
this.customer = customer;
}
public String getLoadingCompanyName() {
return loadingCompanyName;
}
public void setLoadingCompanyName(String loadingCompanyName) {
this.loadingCompanyName = loadingCompanyName;
}
public String getLoadingCity() {
return loadingCity;
}
public void setLoadingCity(String loadingCity) {
this.loadingCity = loadingCity;
}
public String getLoadingPostcode() {
return loadingPostcode;
}
public void setLoadingPostcode(String loadingPostcode) {
this.loadingPostcode = loadingPostcode;
}
public String getDateOfLoading() {
return dateOfLoading;
}
public void setDateOfLoading(String dateOfLoading) {
this.dateOfLoading = dateOfLoading;
}
public String getUnloadingCompanyName() {
return unloadingCompanyName;
}
public void setUnloadingCompanyName(String unloadingCompanyName) {
this.unloadingCompanyName = unloadingCompanyName;
}
public String getUnloadingCity() {
return unloadingCity;
}
public void setUnloadingCity(String unloadingCity) {
this.unloadingCity = unloadingCity;
}
public String getUnloadingPostcode() {
return unloadingPostcode;
}
public void setUnloadingPostcode(String unloadingPostcode) {
this.unloadingPostcode = unloadingPostcode;
}
public String getDateOfUnloading() {
return dateOfUnloading;
}
public void setDateOfUnloading(String dateOfUnloading) {
this.dateOfUnloading = dateOfUnloading;
}
public Double getNettoPrice() {
return nettoPrice;
}
public void setNettoPrice(Double nettoPrice) {
this.nettoPrice = nettoPrice;
}
public Double getBruttoPrice() {
return bruttoPrice;
}
public void setBruttoPrice(Double bruttoPrice) {
this.bruttoPrice = bruttoPrice;
}
public CompanyEntity getCompanyDetails() {
return companyDetails;
}
public void setCompanyDetails(CompanyEntity companyDetails) {
this.companyDetails = companyDetails;
}
public String getInformation() {
return information;
}
public void setInformation(String information) {
this.information = information;
}
public Boolean getPaymentStatus() {
return paymentStatus;
}
public void setPaymentStatus(Boolean paymentStatus) {
this.paymentStatus = paymentStatus;
}
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
}
Below I show you how response result like from Postman.
As u can see the variable 'photo' is as a null. How can i fix it to get it as a for example byte64string
Below is OrderAllUsersResponse class code:
CodePudding user response:
For modelMapper's default mapping mechanism, your source (OrderEntity) and destination (OrderAllUsersResponse) objects should be similar to each other.
@Entity(name = "cargo")
public class OrderEntity implements Serializable {
@Lob
@Column(name = "photo", columnDefinition="BLOB")
private byte[] data;
public byte[] getData() {
return data;
}
public void setData(byte[] data) {
this.data = data;
}
}
and
import com.fasterxml.jackson.annotation.JsonProperty;
public class OrderAllUsersResponse {
@JsonProperty("photo")
private byte[] data;
public byte[] getData() {
return data;
}
public void setData(byte[] data) {
this.data = data;
}
}
If the source field name is different from the destination field name then to define property mappings, you will use ModelMapper's TypeMap.
@Entity(name = "cargo")
public class OrderEntity implements Serializable {
@Lob
@Column(name = "photo", columnDefinition="BLOB")
private byte[] data;
public byte[] getData() {
return data;
}
public void setData(byte[] data) {
this.data = data;
}
}
and
public class OrderAllUsersResponse {
private byte[] photo;
public byte[] getPhoto() {
return photo;
}
public void setPhoto(byte[] photo) {
this.photo = photo;
}
}
TypeMaping
@GetMapping(path = "/getallorders")
public List<OrderAllUsersResponse> getAllOrders() {
List<OrderAllUsersResponse> returnValue = new ArrayList<>();
List<OrderEntity> orders = orderRepostiory.findAllOrders();
ModelMapper modelMapper = new ModelMapper();
TypeMap<OrderEntity, OrderAllUsersResponse> propertyMapper = modelMapper.createTypeMap(OrderEntity.class, OrderAllUsersResponse.class);
for (int i=0; i < orders.size(); i ) {
propertyMapper.addMapping(OrderEntity::getData, OrderAllUsersResponse::setPhoto);
returnValue.add(modelMapper.map(orders.get(i), OrderAllUsersResponse.class));
}
return returnValue;
}