gpt4 book ai didi

java - 无法使用 gson.fromJson 反序列化对象

转载 作者:太空宇宙 更新时间:2023-11-04 13:24:04 27 4
gpt4 key购买 nike

我正在尝试使用 gson.fromJson 反序列化由 postman 发送的对象。

 @POST
@Path( "company" ) // takes name as a path parameter
@Produces( "application/json" ) // response formatted as JSON
public Response insertCompany(String content) {
Gson gson = new Gson();
CompanyWithNoIdReturn newCompany = gson.fromJson(content, CompanyWithNoIdReturn.class);
}

public class CompanyWithNoIdReturn {

private final String name;
private final String description;
private final String logo;

public CompanyWithNoIdReturn(String name, String description, String logo)
{
this.name = name;
this.description = description;
this.logo = logo;
}

public String getName() {

return name;
}

public String getDescription() {

return description;
}

public String getLogo() {

return logo;
}

}

出现的错误是:javax.servlet.ServletException:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1

我知道这意味着它希望以 { 开头,而不是 here 中提到的 " 开头。虽然我不明白的是如何解决它?

json 的构造如下: enter image description here

编辑:使用表单-数据时,内容如下:

content = (java.lang.String) "------WebKitFormBoundaryUG5yy1gW9G2V5qcz
Content-Disposition: form-data; name="CompanyName"

newCompanyName2
------WebKitFormBoundaryUG5yy1gW9G2V5qcz
Content-Disposition: form-data; name="Description"

newDescription2
------WebKitFormBoundaryUG5yy1gW9G2V5qcz
Content-Disposition: form-data; name="logo"

newLogo2
------WebKitFormBoundaryUG5yy1gW9G2V5qcz--
"

最佳答案

我认为您期望 JSON 格式的正文(内容变量)。但根据您的 postman 图像,您正在发送表单数据

尝试切换到 raw 并插入以下内容:

{
"name": "Company name",
"description": "Company description",
"logo": "logo"
}

关于java - 无法使用 gson.fromJson 反序列化对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32816956/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com