简体   繁体   中英

Exception throw when try to parse Json to Java Object with Gson Library

im trying create a web method whos get a Json string. I need to get that json string and parse to a java object.

When im trying to do that i get that exception:

javax.servlet.ServletException: com.google.gson.JsonSyntaxException: 
java.io.EOFException: End of input at line 1 column 2

root cause

com.google.gson.JsonSyntaxException: java.io.EOFException: End of input at 
line 1 column 2

root cause

 java.io.EOFException: End of input at line 1 column 2

And is that my code:

@GET
@Produces("application/text")
@Path("checkuser/{user}")
public String checkUser(@PathParam("user") String mu) throws SQLException, ClassNotFoundException {

    gson = new Gson();
    modelUserGet = gson.fromJson(mu, ModelUser.class);

    StringBuilder query = new StringBuilder();

    query.append("SELECT user, password, email,");
    query.append(" telephone, creation_data, last_update_data ");
    query.append("FROM user ");
    query.append("WHERE user ='");
    query.append(modelUserGet.getUser());
    query.append("' ");

    datamysql = new DataMySqlAccess();
    Statement st = datamysql.getConnection().createStatement();
    ResultSet rs = st.executeQuery(query.toString());



    String result = "";

    if(rs.next() == true){

        modelUserSend = new ModelUser(
                rs.getString("user"),
                rs.getString("password"),
                rs.getString("email"),
                rs.getString("telephone"),
                rs.getString("creation_data"),
                rs.getString("last_update_data")
        );

        if(!modelUserSend.getPassword().equals(modelUserGet.getPassword()))
            result = "INVALID_PASSWORD";
        else
            result = "OK";
    } else 
        result = "INVALID_USER";


    modelUserSend.setCheckUserReponse(result);

    return gson.toJson(modelUserSend);
}

And this is my model class

private String user;
private String password;
private String email;
private String telephone;
private String creationData;
private String lastUpdateData;
private String checkUserResponse;

public ModelUser(String user, String password, String email, String telephone, String creationData, String lastUpdateData){
    this.user = user;
    this.password = password;
    this.email = email;
    this.telephone = telephone;
    this.creationData = creationData;
    this.lastUpdateData = lastUpdateData;
}

And im sending that parameter json string

{
    "user":"admin",
    "password": "admin",
    "email":"admin@admin.com",
    "telephone":"(11) 3761-5292",
    "creationData":"2018-07-08",
    "lastUpdateData":"2018-07-08"
}

Im i doing something wrong?

Thx. o/

@PathParam("user") String mu

This is the PathParameter you are getting, which is a String. That is the reason for the exception. It could not convert to model.

In your case, make it as separate path param variable and use it in the method.

Edit 1

I think, user name is the only item you need from the param. So, its better to fetch the username as Path param. And other details, you are the getting from the database.

@GET
@Produces("application/text")
@Path("checkuser/{user}")
public String checkUser(@PathParam("user") String userName) throws SQLException, ClassNotFoundException {

StringBuilder query = new StringBuilder();

query.append("SELECT user, password, email,");
query.append(" telephone, creation_data, last_update_data ");
query.append("FROM user ");
query.append("WHERE user ='");
query.append(userName);
query.append("' ");

datamysql = new DataMySqlAccess();
Statement st = datamysql.getConnection().createStatement();
ResultSet rs = st.executeQuery(query.toString());



String result = "";

if(rs.next() == true){

    modelUserSend = new ModelUser(
            rs.getString("user"),
            rs.getString("password"),
            rs.getString("email"),
            rs.getString("telephone"),
            rs.getString("creation_data"),
            rs.getString("last_update_data")
    );

    if(!modelUserSend.getPassword().equals(modelUserGet.getPassword()))
        result = "INVALID_PASSWORD";
    else
        result = "OK";
} else 
    result = "INVALID_USER";


modelUserSend.setCheckUserReponse(result);

return gson.toJson(modelUserSend);

}

With that code i can send json, get that json and cast to JavaObject, to work better than of String parameters.

@GET
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("checkuser/{user}")
public String checkUser(@PathParam("user") String mu) throws SQLException, 
ClassNotFoundException {

    gson = new Gson();
    modelUserGet = gson.fromJson(mu, ModelUser.class);

    StringBuilder query = new StringBuilder();

    query.append("SELECT user, password, email,");
    query.append(" telephone, creation_data, last_update_data ");
    query.append("FROM user ");
    query.append("WHERE user ='");
    query.append(modelUserGet.getUser());
    query.append("' ");

    datamysql = new DataMySqlAccess();
    Statement st = datamysql.getConnection().createStatement();
    ResultSet rs = st.executeQuery(query.toString());

    String result = "";

    if(rs.next() == true){

        modelUserSend = new ModelUser(
                rs.getString("user"),
                rs.getString("password"),
                rs.getString("email"),
                rs.getString("telephone"),
                rs.getString("creation_data"),
                rs.getString("last_update_data")
        );

        if(!modelUserSend.getPassword().equals(modelUserGet.getPassword()))
            result = "INVALID_PASSWORD";
        else
            result = "OK";
    } else 
        result = "INVALID_USER";


    modelUserSend.setCheckUserReponse(result);

    return gson.toJson(modelUserSend);
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM