POST http:// localhost:8080 / server2_0_war_exploded / user返回的响应状态为500内部服务器错误

时间:2016-12-07 16:34:36

标签: java json intellij-idea gson

您好我在尝试创建用户时遇到此错误,希望有人可以提供帮助。其他功能就像一个魅力,但不是这个。 有一个客户端和一个服务器。

提前谢谢你:D

客户端控制器中的createUser()

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

}

客户端HttpRequests类中的Post方法

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

Server DbConnection类中的Prepared语句

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

输入到Api

欢迎来到主菜单

您有以下选择

登录

创建用户

2

输入您的名字

克里斯托弗

输入您的姓氏

Palsgaard

输入您的电子邮件

Chris@chris.com

输入您的用户名

好朋友

输入您的密码

1234

错误

尝试调试客户端连接类第132行

时出错
        if (clientResponse.getStatus() == 200) { clientResponse: "POST http://localhost:8080/server2_0_war_exploded/user returned a response status of 500 Internal Server Error"

服务器错误日志

[2016-12-07T17:28:05.007 + 0100] [glassfish 4.1] [警告] [] [javax.enterprise.web] [tid:_ThreadID = 27 _ThreadName = http-listener-1(3)] [ timeMillis:1481128085007] [levelValue:900] [[   StandardWrapperValve [MyApplication]:servlet的Servlet.service()MyApplication引发了异常 com.google.gson.stream.MalformedJsonException:使用JsonReader.setLenient(true)接受第1行第5列路径的格式错误的JSON $     在com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1559)     在com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1401)     在com.google.gson.stream.JsonReader.doPeek(JsonReader.java:542)     在com.google.gson.stream.JsonReader.peek(JsonReader.java:425)     在com.google.gson.Gson.assertFullConsumption(Gson.java:859)     在com.google.gson.Gson.fromJson(Gson.java:853)     在com.google.gson.Gson.fromJson(Gson.java:801)     在com.google.gson.Gson.fromJson(Gson.java:773)     在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)     在org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)     在org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)     在org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)     在org.glassfish.jersey.server.ServerRuntime $ 2.run(ServerRuntime.java:309)     在org.glassfish.jersey.internal.Errors $ 1.call(Errors.java:271)     在org.glassfish.jersey.internal.Errors $ 1.call(Errors.java:267)     在org.glassfish.jersey.internal.Errors.process(Errors.java:315)     在org.glassfish.jersey.internal.Errors.process(Errors.java:297)     在org.glassfish.jersey.internal.Errors.process(Errors.java:267)     在org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)     在org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:292)     在org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1139)     在org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:460)     在org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:386)     在org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:334)     在org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)     在org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)     在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)     在org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)     在org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)     在com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)     在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)     在org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)     在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)     在org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)     在org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)     在org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)     在org.glassfish.grizzly.filterchain.ExecutorResolver $ 9.execute(ExecutorResolver.java:119)     在org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)     在org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)     在org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)     在org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)     在org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)     在org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)     在org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)     在org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)     在org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access $ 100(WorkerThreadIOStrategy.java:56)     在org.glassfish.grizzly.strategies.WorkerThreadIOStrategy $ WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)     在org.glassfish.grizzly.threadpool.AbstractThreadPool $ Worker.doWork(AbstractThreadPool.java:591)     在org.glassfish.grizzly.threadpool.AbstractThreadPool $ Worker.run(AbstractThreadPool.java:571)     在java.lang.Thread.run(Thread.java:745) ]

1 个答案:

答案 0 :(得分:0)

您当前的代码:

客户端(生成json文本,加密并发送):

    ClientResponse clientResponse = HttpRequests.post("/user/", Crypter.encryptDecryptXOR(new Gson().toJson(data)));

服务器端(解析json,然后解密):

String s = new Gson().fromJson(data,String.class);
String decrypt = Crypter.encryptDecryptXOR(s);

...看起来你应该解密之前你试图解析JSON数据(因此解析收到的json时会出现MalformedJsonException)。

顺便说一句,String.class可能不是你想要将json内容反序列化的(User.class怎么样?)

编辑:

对于解密,请尝试以下方法:

String decrypted = Crypter.encryptDecryptXOR(data); 
SomeClass userData = new Gson().fromJson(decrypted, SomeClass.class);

SomeClass 是一个可以容纳json内容的类(用户可能是一个很好的候选者,但你需要检查一下)。