我试图创建一个获取Json字符串的Web方法。我需要获取json字符串并将其解析为java对象。
当我试图这样做时,我得到了异常:
javax.servlet.ServletException: com.google.gson.JsonSyntaxException:
java.io.EOFException: End of input at line 1 column 2
根本原因
com.google.gson.JsonSyntaxException: java.io.EOFException: End of input at
line 1 column 2
根本原因
java.io.EOFException: End of input at line 1 column 2
那是我的代码吗?
@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);
}
这是我的模型课
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;
}
然后我发送该参数json字符串
{
"user":"admin",
"password": "admin",
"email":"admin@admin.com",
"telephone":"(11) 3761-5292",
"creationData":"2018-07-08",
"lastUpdateData":"2018-07-08"
}
我做错什么了吗?
Thx。 o /
答案 0 :(得分:0)
@PathParam(“ user”)字符串mu
这是您要获取的PathParameter,它是一个字符串。这就是例外的原因。无法转换为模型。
在您的情况下,请将其作为单独的路径参数变量并在方法中使用它。
编辑1
我认为,用户名是您从参数中唯一需要的项目。因此,最好将用户名作为Path参数来获取。还有其他细节,您就是从数据库中获得的。
def f(arr):
best_val = calc_x(arr)
used = set(arr)
for i, v in enumerate(arr):
for replacement in used:
if replacement == v:
continue
arr2 = arr[0:i] + replacement + arr[i:]
y = abs(replacement - v)
x = calc_x(arr2)
best_val = min(best_val, x + y)
return best_val
}
答案 1 :(得分:0)
使用该代码,我可以发送json,获取该json并将其转换为JavaObject,以比String参数更好地工作。
@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);
}