2016-10-13 37 views
0
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); 
    } 
} 

這是客戶端代碼的響應狀態...我工作的JSON和得到錯誤返回400錯誤請求

@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; 
    } 
} 

這是JSON代碼

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; 
    } 
} 

其中一個命中分貝...

而輸出是

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 

請幫我看看這裏..怎麼了?

+0

你應該檢查你的行:'String output = response.toString();'。看起來這不會返回JSON字符串,而是返回其他內容。 – sanastasiadis

回答

0

看來您需要直接處理對象而無需在JSON中進行轉換。你可以改變之後的參數:

ClientResponse response = webResource.type("application/json") 
            .post(ClientResponse.class, json); 

要直接發送的對象,而不是JSON,如:

ClientResponse response = webResource.type("application/json") 
            .post(ClientResponse.class, addRequest); 

或者,在你的服務器端,您可以添加,需要一個String服務作爲輸入參數:

@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; 
} 
+0

嗨@sanastasiadis,謝謝你的迴應。 –

+0

我做了你所建議的更改。我仍然得到錯誤,它是這樣的... –

+0

線程「主」的異常com.sun.jersey.api.client.ClientHandlerException:com.sun.jersey.api.client.ClientHandlerException:一個消息正文編寫器Java類型,類com.infy.service.EmployeeRequest和MIME媒體類型,應用程序/ JSON,沒有被發現 –

相關問題