gpt4 book ai didi

java - 当给定字符串数值时,响应返回 html 而不是 Java 中的 json

转载 作者:行者123 更新时间:2023-12-01 18:13:45 25 4
gpt4 key购买 nike

我尝试通过下面的代码发送一个对象,我已经在函数之外处理了客户端和Webtarget部分代码。

public Result setUserDetails(UserDetails userDetails) {
if(userDetails.getAge()==null||userDetails.getId()==null||userDetails.getname()==null) {
return new Result(201,"null fields","objects field cannot be null","null filed");
}else {
Invocation.Builder invocationBuilder = webTarget.request();
Response response = invocationBuilder.post(Entity.entity(userDetails, MediaType.APPLICATION_JSON));
String jsonResponse = response.readEntity(String.class);
// System.out.println(jsonResponse);
Gson gson = new Gson();
Result result = gson.fromJson(jsonResponse, Result.class);
return result;
}

}

该对象具有基本属性,但全部都是字符串,如下所示,在 UserDetails 类中

package com.jay;

public class UserDetails {

String id;
String name;
String age;
public String getId() {
return id;
}

public String getname() {
return name;
}

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

public void setname(String name) {
this.name = name;
}

public String getAge() {
return age;
}

public void setAge(String age) {
this.age = age;
}

public UserDetails(String id, String name, String age) {
super();
this.id = id;
this.name = name;
this.age = age;
}

public UserDetails() {
super();
}
}

当我尝试像这样发送 jaydev 作为年龄值时

UserDetails insertUser = new UserDetails("31211","jay_dev","jaydev");

Result postResult = clientSidedRequest.setUserDetails(insertUser);

我得到了 html 形式的响应,而不是 json,其中作为客户端和服务器,userdetails 类具有相同的字段数据类型,即 string

jsonResponse 填充了 html 数据而不是字符串。

我不明白为什么会发生这种情况,因为我是新手。另外,也没有错误,只是我无法反序列化 jsonResponse

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Request Error</title>
<style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
</head>
<body>
<div id="content">
<p class="heading1">Request Error</p>
<p xmlns="">The server encountered an error processing the request. Please see the <a rel="help-page" href="http://192.168.1.22:8080/help">service help page</a> for constructing valid requests to the service.</p>
</div>
</body>
</html>

这是我打印 jsonResponse 时得到的结果,当我尝试使用 gson 反序列化时出现错误

Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:226)
at com.google.gson.Gson.fromJson(Gson.java:927)
at com.google.gson.Gson.fromJson(Gson.java:892)
at com.google.gson.Gson.fromJson(Gson.java:841)
at com.google.gson.Gson.fromJson(Gson.java:813)
at com.jay.ClientSidedRequest.setUserDetails(ClientSidedRequest.java:49)
at com.jay.Main.main(Main.java:13)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:215)
... 6 more

最佳答案

当您从客户端应用程序发送数据时,请确保您具有 http header

Content-Type: application/json

并用

注释您的请求处理方法
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})

关于java - 当给定字符串数值时,响应返回 html 而不是 Java 中的 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60413566/

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