当字符串被赋予数值时,响应返回html而不是Java中的json
我试图通过下面的代码发送一个对象,我已经处理了客户端和webtarget部分函数之外的代码当字符串被赋予数值时,响应返回html而不是Java中的json,java,rest,http,jersey,Java,Rest,Http,Jersey,我试图通过下面的代码发送一个对象,我已经处理了客户端和webtarget部分函数之外的代码 public Result setUserDetails(UserDetails userDetails) { if(userDetails.getAge()==null||userDetails.getId()==null||userDetails.getname()==null) { return new Result(201,"null fields","ob
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
class中所示
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
类对于字段iestring
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>
从客户端应用程序发送数据时,请确保您具有http头 内容类型:application/json 并用
您是否可以将输出也共享给其他人以验证您的响应您从服务器收到错误页面。检查服务日志中是否存在任何异常。如果未显示任何异常,请使用通用ExceptionMapper尝试记录任何异常。
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
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})