我有一個模型類,如下所示:
package com.example.model;
import java.util.Map;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import com.example.helpers.StringMapConverter;
@Entity
@Table(name = "buildingcompanies")
public class Buildcompanies {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name = "shortname")
private String shortname;
@Column(name = "fullname")
private String fullname;
@Column(name = "address")
private String address;
@Column(name = "telephone")
private String telephone;
@Column(name = "website")
private String website;
@Column(name = "sociallinks")
@Convert(converter = StringMapConverter.class)
private Map<String, String> sociallinks;
@Column(name = "foundationyear")
private String foundationyear;
public Buildcompanies() {
}
public Buildcompanies(String shortname, String fullname, String address, String telephone, String website,
Map<String, String> map, String foundationyear) {
this.shortname = shortname;
this.fullname = fullname;
this.address = address;
this.telephone = telephone;
this.website = website;
this.sociallinks = map;
this.foundationyear = foundationyear;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getShortname() {
return shortname;
}
public void setShortname(String shortname) {
this.shortname = shortname;
}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
public Map<String, String> getSociallinks() {
return sociallinks;
}
public void setSociallinks(Map<String, String> sociallinks) {
this.sociallinks = sociallinks;
}
public String getFoundationyear() {
return foundationyear;
}
public void setFoundationyear(String foundationyear) {
this.foundationyear = foundationyear;
}
}
控制器中顯示輸出的方法:
public ResponseEntity<List<Buildcompanies>> getAllCompanies(@RequestParam(required = false) String name) {
try {
List<Buildcompanies> companies = new ArrayList<Buildcompanies>();
int test=0;
if (name == null)
{
buildcompaniesRepository.findAll().forEach(companies::add);
}
else
buildcompaniesRepository.findByShortnameContaining(name).forEach(companies::add);
if (companies.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>(companies, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
它確實顯示了一個輸出,一切都很好:
[
{
"id": 81,
"shortname": "testing",
"fullname": "test",
"address": "addrtest",
"telephone": "380979379992",
"website": "www.site.com",
"sociallinks": {
"facebook": "fb.com"
},
"foundationyear": "1991"
}
]
我想在向最終用戶顯示資料的同時計算每個公司的評級。所以輸出應該如下:
[
{
"id": 81,
"shortname": "testing",
"fullname": "test",
"address": "addrtest",
"telephone": "380979379992",
"website": "www.site.com",
"sociallinks": {
"facebook": "fb.com"
},
"foundationyear": "1991",
"rating": "1.5"
}
]
是否可以將評級列動態添加到公司串列中,或者我應該在資料庫中創建評級列,在控制器中為其更新方法,迭代 findAll() 結果并在每次用戶嘗試訪問 /list 端點時呼叫它?
uj5u.com熱心網友回復:
你有兩個選擇:
為此,您可以在 Buildcompanies 類中引入一個新屬性并使用 @Transient 對其進行注釋。這將表示該屬性不需要保留在資料庫中,并且 JPA 不會嘗試在表中創建列。
推薦的方法是不使用 Entity 類作為回應物件。理想情況下,您應該有一個域物件,并且資料庫回應應該映射到該物件。映射時,您可以應用要添加到其中的任何自定義詳細資訊。
uj5u.com熱心網友回復:
只需將@Transient注釋添加到您的動態欄位。資料庫中不需要相應的列。瞬態列的值只存在于運行時。
通常,出于多種原因,將物體作為 JSON 與外部系統共享是一個壞主意。請改用中間 DTO。有很多庫允許從物體到 DTO 的可配置自動映射(ModelMapper對我來說非常好)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/409407.html
標籤:
