Reputation: 51
Hi there I am getting this error when try to create a user, hope someone can help. Other functions work like a charm but not this one. There is a Client and a server.
Thank you in advance :D
createUser() in the Client Controller
public void createUser() {
JsonObject data = new JsonObject();
Scanner input = new Scanner(System.in);
System.out.println("Input your firstname");
data.addProperty("firstname", input.nextLine());
System.out.println("Input your Lastname");
data.addProperty("lastname", input.nextLine());
System.out.println("Input your Email");
data.addProperty("email", input.nextLine());
System.out.println("Input your Username");
data.addProperty("username", input.nextLine());
System.out.println("Input your Password");
data.addProperty("password", input.nextLine());
data.addProperty("usertype", "0");
Connection.postUser(data);
}
PostUser() in Client Connection Class
public static String postUser(JsonObject data) {
ClientResponse clientResponse = HttpRequests.post("/user/", Crypter.encryptDecryptXOR(new Gson().toJson(data)));
String response = null;
if (clientResponse == null) {
System.out.println("Error on SDK");
} else {
response = clientResponse.getEntity(String.class);
if (clientResponse.getStatus() == 200) {
System.out.println(response);
} else
System.out.println("error");
}
clientResponse.close();
return response;
}
Post method in Client HttpRequests class
public static ClientResponse post(String path, String data) {
ClientResponse clientResponse = null;
try {
WebResource webResource = client
.resource("http://localhost:8080/server2_0_war_exploded")
.path(path);
clientResponse = webResource.accept("application/json").post(ClientResponse.class, data);
}
catch
(UniformInterfaceException | ClientHandlerException e) {
e.printStackTrace();
}
return clientResponse;
}
Post method Server endpoint
@POST
@Produces("application/json")
public Response create(String data) throws Exception {
String s = new Gson().fromJson(data,String.class);
String decrypt = Crypter.encryptDecryptXOR(s);
if (controller.addUser(decrypt)) {
return Response
.status(200)
.entity("{\"message\":\"Success! User added\"}")
.build();
}
else return Response.status(400).entity("{\"message\":\"failed\"}").build();
}
Prepared statement in Server DbConnection class
public boolean addUser(User u) throws Exception {
PreparedStatement addUserStatement =
conn.prepareStatement("INSERT INTO Users (First_Name, Last_Name, Username, Email, Password, Usertype) VALUES (?, ?, ?, ?, ?, ?)");
try {
addUserStatement.setString(1, u.getFirstName());
addUserStatement.setString(2, u.getLastName());
addUserStatement.setString(3, u.getUsername());
addUserStatement.setString(4, u.getEmail());
addUserStatement.setString(5, u.getPassword());
addUserStatement.setBoolean(6, u.getUserType());
addUserStatement.execute();
} catch (SQLException e) {
e.printStackTrace();
}
return true;
}
Input to Api
Welcome to the mainmenu
You have the following choices
Login
Create user
2
Input your firstname
Christoffer
Input your Lastname
Palsgaard
Input your Email
Input your Username
pals
Input your Password
1234
error
Error when i try to debug Client Connection class line 132
if (clientResponse.getStatus() == 200) { clientResponse: "POST http://localhost:8080/server2_0_war_exploded/user returned a response status of 500 Internal Server Error"
And the server error log
[2016-12-07T17:28:05.007+0100] [glassfish 4.1] [WARNING] [] [javax.enterprise.web] [tid: _ThreadID=27 _ThreadName=http-listener-1(3)] [timeMillis: 1481128085007] [levelValue: 900] [[ StandardWrapperValve[MyApplication]: Servlet.service() for servlet MyApplication threw exception com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 5 path $ at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1559) at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1401) at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:542) at com.google.gson.stream.JsonReader.peek(JsonReader.java:425) at com.google.gson.Gson.assertFullConsumption(Gson.java:859) at com.google.gson.Gson.fromJson(Gson.java:853) at com.google.gson.Gson.fromJson(Gson.java:801) at com.google.gson.Gson.fromJson(Gson.java:773) at endpoints.UsersEndpoint.create(UsersEndpoint.java:113) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81) at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144) at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161) at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:160) at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99) at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389) at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347) at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102) at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:309) at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271) at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267) at org.glassfish.jersey.internal.Errors.process(Errors.java:315) at org.glassfish.jersey.internal.Errors.process(Errors.java:297) at org.glassfish.jersey.internal.Errors.process(Errors.java:267) at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317) at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:292) at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1139) at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:460) at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:386) at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:334) at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221) at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673) at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174) at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283) at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167) at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206) at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180) at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235) at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200) at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132) at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111) at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536) at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137) at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591) at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571) at java.lang.Thread.run(Thread.java:745) ]]
Upvotes: 0
Views: 1165
Reputation: 131
Your current code:
Client side (generate json text, encrypt it, and send):
ClientResponse clientResponse = HttpRequests.post("/user/", Crypter.encryptDecryptXOR(new Gson().toJson(data)));
Server side (parse json, then decrypt):
String s = new Gson().fromJson(data,String.class);
String decrypt = Crypter.encryptDecryptXOR(s);
... looks like you should be decrypting before you try to parse the JSON data (hence the MalformedJsonException when parsing received json).
And by the way, String.class
is probably not what you want the json content to be deserialized to (what about User.class
?)
Edit:
For decryption, try something like:
String decrypted = Crypter.encryptDecryptXOR(data);
SomeClass userData = new Gson().fromJson(decrypted, SomeClass.class);
SomeClass is a class that can accomodate the content of your json (User might be a good candidate, but you will need to check this).
Upvotes: 0