gpt4 book ai didi

java - 415 POST 请求中不支持的媒体类型

转载 作者:行者123 更新时间:2023-11-28 22:50:49 28 4
gpt4 key购买 nike

正在四处阅读,其他人遇到的很多问题都是 Spring、Gradle 或其他问题。 Postman 似乎不喜欢我对 POST 方法提出的请求。

错误:

b>description</b>
<u>The server refused this request because the request entity is in a format not supported by the requested resource for the requested method (Unsupported Media Type).</u>

来自 Postman 的正文:

{
"firstName": "firstName",
"lastName": "lastName",
"dob": "12/12/2012",
"address": "123 main st."
}

有问题的帖子:

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/createQuery")
public Response createQuery(ApplicationInfo info) throws IOException {
String queryString =createStringFromJSON(info,true);
queryMap.put(""+countId,queryString);
countId++;
ObjectMapper objectMapper = new ObjectMapper();
String json = "";
try {
json = objectMapper.writeValueAsString(queryMap);
} catch (JsonProcessingException e) {
e.printStackTrace();
} return Response.ok(json,MediaType.APPLICATION_JSON_TYPE).entity(json).build();
}

极其复杂的 ApplicationInfo 对象:

 public class ApplicationInfo

{
String firstName;
String lastName;
String dob;
String address;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getDob() {
return dob;
}

public void setDob(String dob) {
this.dob = dob;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}
}

希望更有经验的人可以查明问题所在。请保持它与我的问题相关。

最佳答案

您的请求正文似乎是有效的 json 字符串,只需验证您是否在 postman 应用程序中传递了正确的 header
header 看起来像

Accept:application/json
Content-Type:application/json

enter image description here

关于java - 415 POST 请求中不支持的媒体类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40116780/

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