簡體   English   中英

我正在處理json並收到錯誤,返回響應狀態為400 Bad Request

[英]I am working on json and getting the error returned a response status of 400 Bad Request

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

這是客戶代碼...

@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

請在這里幫助我。.怎么了?

似乎您需要直接處理對象,而無需在JSON中進行轉換。 您可以更改post參數:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM