英文:
Jackson same value for two different attributes
问题
在Spring 1.4.5应用程序中,我在控制器中接收数据,代码如下:
@PostMapping(value = ENDPOINT, consumes = "application/json")
public ResponseEntity<?> post(@RequestBody Email email) {
其中,Email类有一个属性:
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
@Email
private String mail;
由于属性上的READ_ONLY
,尽管控制器接收到了邮件(我通过拦截器进行了检查),但控制器不会将其选取。
我尝试通过新属性来复制mail
:
@JsonProperty("mail")
private String from;
但是我遇到了JsonMappingException
,提示"Conflicting getter definitions for property"。
如何才能提取传入JSON中的邮件呢?
英文:
In a Spring 1.4.5 application I receive data in a Controller with:
@PostMapping(value = ENDPOINT, consumes= "application/json")
public ResponseEntity<?> post(@RequestBody Email email) {
Where Email has an attribute
@JsonPropery(access = JsonProperty.Access.READ_ONLY)
@Email
private String mail;
The Controller receives a mail (I checked with an Interceptor) but it is not picked in the Controller due to the READ_ONLY that I do not dare to remove (it's been in production for years).
I tried to duplicate mail with a new attribute
@JsonPropery("mail")
private String from;
but I get JsonMappingException Conflicting getter definitions for property
How can I pick the mail existing in the incoming json?
答案1
得分: 0
已经行之有效的解决方案是不创建新属性的getter方法:
@JsonPropery(access = JsonProperty.Access.READ_ONLY)
@Email
private String mail;
@JsonPropery("mail")
private String from;
private String getMail() {
return mail;
}
private void setMail(String mail){
this.mail = mail;
}
private String calcFrom() {
return from;
}
private void setFrom(String from) {
this.from = from;
}
英文:
The solution that has worked has been not creating a getter for the new propery:
@JsonPropery(access = JsonProperty.Access.READ_ONLY)
@Email
private String mail;
@JsonPropery("mail")
private String from;
private String getMail() {
return mail;
}
private void setMail(String mail){
this.mail = mail;
}
private String calcFrom() {
return from;
}
private void setFrom(String from) {
this.from = from;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论