나는 똑같은 것을 찾고 있었다.
는 Jackson docs에 따르면, 우리는 속성, 게터 또는 세터에 @JsonIgnore
주석을 설정하고 우리가 세터에게 그것을 설정할 때이 속성을 볼 수 있는지 확인하십시오 것 @JsonProperty
주석을 제공하지 않는 즉, 전체 재산을 숨길 것입니다. 불행히도, 다른 방법으로는 작동하지 않습니다. 내 응답에이 속성을 표시하고 싶지만 내 요청에는 속성을 포함시키지 않았습니다.
사용 사례는 자동 예를 들어 ID를 생성합니다. 사용자가 설정하거나보기를 원하지 않지만 읽을 수 있습니다.
당신은 실제로 속성 자체를 숨겨이를 다음 @JsonAnyGetter
방법을 추가 할 수 있습니다. 이 방법에서는 데이터를 원하는 형식으로 마사지 할 수 있습니다. 하나의 이름이나 식별자 또는 기타 형식을 표시하려는 복잡한 속성 클래스를 나타내는 것도 유용합니다. 후자를 수행하는 더 좋은 방법이 있지만 유스 케이스가 너무 복잡하지 않으면 충분하다.
그래서 예로서 (나의 사과이 너무 정교 경우) :
사용자 :
public class User {
private String uid;
private String customerId;
private String taxSsnId;
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getCustomerId() {
return extCustomerId;
}
public void setCustomerId(String extCustomerId) {
this.extCustomerId = extCustomerId;
}
public String getTaxSsnId() {
return taxSsnId;
}
public void setTaxSsnId(String taxSsnId) {
this.taxSsnId = taxSsnId;
}
@JsonIgnore
public void getId(){
if (getUid() != null){
return getUid();
}
if (getCustomerId() != null){
return getCustomerId();
}
return null;
}
}
설정 :
public class Setting {
@JsonIgnore
private int id;
private String key;
private String value;
@JsonIgnore
private User lastUpdatedBy;
@JsonIgnore
private Date lastUpdatedAt;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public User getLastUpdatedBy() {
return lastUpdatedBy;
}
public void setLastUpdatedBy(User lastUpdatedBy) {
this.lastUpdatedBy = lastUpdatedBy;
}
public Date getLastUpdatedAt() {
return lastUpdatedAt;
}
public void setLastUpdatedAt(Date lastUpdatedAt) {
this.lastUpdatedAt = lastUpdatedAt;
}
@JsonAnyGetter
private Map<String, String> other() {
Map<String, String> map = new LinkedHashMap<String, String>();
map.put("id", this.getId());
map.put("lastUpdatedBy", this.getLastUpdatedBy().getId());
SimpleDateFormat format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
map.put("lastUpdatedAt", format.format(this.getLastUpdatedAt()));
return map;
}
}
이 Request
스키마 (역 직렬화보기)를 나타냅니다 :
{
"key": "string",
"value": "string"
}
(210)
이 Response
스키마 (연재보기) :
{
"key": "string",
"value": "string",
"id": "12345",
"lastUpdatedBy": "String",
"lastUpdatedAt": "String"
}
가 어떻게 클래스에서 세터없이 속성 값을 역 직렬화 것인가? – eis
deserialization에 대해서는 신경 쓰지 않습니다. 역 직렬화 할 때 무시할 수 있습니다. – Trant
음 ...하지만 당신은 "클라이언트 측에서 getter 값을 사용할 수 있기를 원합니다."라고 말하고 있습니다. 클라이언트 자바 스크립트가 사용자에게 보낸 json에서 사용할 수 있으므로 값을 필요로합니다. 즉, 비 직렬화 할 때 필요하지 않습니다. JSON -> 자바) 직렬화 할 때 (자바 -> JSON)? – eis