REST API在POSTMAN中的JSON响应:其中一个字段的值在响应中显示为null。

huangapple go评论152阅读模式
英文:

REST API JSON response in POSTMAN: one field is coming as null in response

问题

在使用JPA将实体保存到DERBY DB时,当我发布JSON并尝试将数据保存到数据库时,响应中的url字段为空,而数据插入正常进行,我没有收到任何错误。

POJO类:

@Entity
public class Message {

    @Id 
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;
    private String title;
    private String content;
    private String sender;
    private String URL;

    public Message(){

    }

    public Message(String URL, Long id, String title, String content, String sender) {
        super();
        this.URL = URL;
        this.id = id;
        this.title = title;
        this.content = content;
        this.sender = sender;
    }

    public String getURL() {
        return URL;
    }

    public void setURL(String uRL) {
        URL = uRL;
    }

    @JsonIgnore
    @JsonProperty(value = "id")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getSender() {
        return sender;
    }

    public void setSender(String sender) {
        this.sender = sender;
    }
}

发布的JSON:

{
    "title": "alpha beta gama",
    "content": "greek letters" ,
    "sender": "laura",
    "URL": "http://abc.efg"
}

响应:

{
    "title": "alpha beta gama",
    "content": "greek letters",
    "sender": "laura",
    "url": null
}
英文:

I am saving this entity using JPA in DERBY DB, whenever I am posting JSON and trying to save data in DB, my url field is coming null in response, while insertion of data is going well and I am not getting any error.

POJO class:

@Entity
public class Message {
@Id 
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private String title;
private String content;
private String sender;
private String URL;
public Message(){
}
public Message(String URL,Long id ,String title, String content, String sender) {
super();
this.URL = URL;
this.id = id;
this.title = title;
this.content = content;
this.sender = sender;
}
public String getURL() {
return URL;
}
public void setURL(String uRL) {
URL = uRL;
}
@JsonIgnore
@JsonProperty(value = "id")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
}

post JSON:

{
"title":"alpha beta gama",
"content":"greek letters" ,
"sender":"laura",
"URL":"http://abc.efg"
}

Response

{
"title": "alpha beta gama",
"content": "greek letters",
"sender": "laura",
"url": null
}

答案1

得分: 1

你尝试使用小写的 "url" 而不是 "URL" 吗?

英文:

Did you try to user lowercase "url" instead of "URL"?

huangapple
  • 本文由 发表于 2020年9月6日 04:55:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63758428.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定