Я пытался отправить объект с помощью приведенного ниже кода, я обработал часть кода клиента и веб-цели вне функции.
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;
}
}
Объект имел базовые свойства c, но все они были строковыми как показано ниже в 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
имеет одинаковый тип данных для полей ie 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