public class DSprojectClient {
public static void main(String[] args) {
Client client = Client.create();
Gson gson = new Gson();
String method = "N2";
WebResource webResource = client.resource("http://localhost:8080/EmployeeManage/demo/DS/" + method);
EmployeeAddRequest addRequest = new EmployeeAddRequest();
addRequest.setId(1103);
String json = gson.toJson(addRequest/* , EmployeeAddRequest.class */);
System.out.println(json);
// String id = "1103";
// String input = "{\"id\":\"1103\"}";
ClientResponse response = webResource.type("application/json").post(ClientResponse.class, json);
System.out.println("response" + response);
System.out.println("Output from Server .... \n");
String output = response.toString();
System.out.println("output\n" + output);
EmployeeResponse addResponse = new EmployeeResponse();
addResponse = gson.fromJson(output, EmployeeResponse.class);
System.out.println(output);
System.out.println(addResponse.getId() + ****************************");
System.out.println(output);
}
}
This is the client code...
@Path("/DS")
public class Standardization {
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/N2")
public EmployeeResponse getDetails1(EmployeeRequest employeeRequest) throws Exception{
DsUserService dsUserService = new DsUserService();
System.out.println("getDetails1::object created");
EmployeeResponse retval = dsUserService.AddEmp(employeeRequest.getId());
return retval;
}
}
this is the json code
public class DsUserService {
Gson gson = new Gson();
PreparedStatement stmt = null;
static Connection conn = null;
static {
try {
Class.forName("com.mysql.jdbc.Driver");
DB_Connector.getInstance();
System.out.println(" Connection made :: "+DB_Connector.connection);
conn = DB_Connector.connection; // Get DB Connection
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public EmployeeResponse AddEmp(int id) throws Exception{
EmployeeResponse retval = new EmployeeResponse();
if ( conn != null ) {
System.out.println(" DB Connection Successful ");
String query = "Select name FROM harish_table_employee WHERE id="+id;
stmt = conn.prepareStatement(query);
ResultSet rs = stmt.executeQuery();
if (rs.next()){
retval.setName(rs.getString("name"));
retval.setId(rs.getInt("id"));
}
}
return retval;
}
}
the one which hits db...
And the output is
Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 5
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
at com.google.gson.Gson.fromJson(Gson.java:795)
at com.google.gson.Gson.fromJson(Gson.java:761)
at com.google.gson.Gson.fromJson(Gson.java:710)
at com.google.gson.Gson.fromJson(Gson.java:682)
at com.infy.service.DSprojectClient.main(DSprojectClient.java:30)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 5
at com.google.gson.stream.JsonReader.expect(JsonReader.java:339)
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:322)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:165)
... 5 more
Please help me out here.. Whats going wrong?
It seems that you need to handle the objects directly without transformations in JSON. You can change the post parameter:
ClientResponse response = webResource.type("application/json")
.post(ClientResponse.class, json);
To send directly the object, and not the json, as:
ClientResponse response = webResource.type("application/json")
.post(ClientResponse.class, addRequest);
Alternatively, at your server side, you can add a service that takes a String as an input parameter:
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/N2")
public EmployeeResponse getDetails2(String employeeRequestJson) throws Exception{
DsUserService dsUserService = new DsUserService();
System.out.println("getDetails1::object created");
EmployeeRequest employeeRequest = gson.fromJson(employeeRequestJson, EmployeeRequest.class);
EmployeeResponse retval = dsUserService.AddEmp(employeeRequest.getId());
return retval;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.