将包含json的HttpServletRequest转换为POJO - java.lang.IllegalStateException

时间:2014-06-24 18:35:57

标签: java json gson

我正在尝试使用海报加载项发送json请求。我发送请求到一个函数来读取json并创建InputDTO对象。这似乎运行find但是当我调用函数来获取赋值时,它会抛出java.lang.NullPointerException错误。我找到了这个教程并尝试使用它。 http://edwin.baculsoft.com/2011/11/how-to-create-a-simple-servlet-to-handle-json-requests/

这似乎正是我所需要的,但它不起作用。以下是我正在做的代码片段。有人可以帮忙吗?

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 try (PrintWriter pw = response.getWriter())
        {
            InputDTO inputJson = XMLReader.inputFromJson(request);
            pw.println("before the call to get timezone from the input DTO");
            pw.println(inputJson.getTimeZone());
            pw.println("after the call to get timezone from the input DTO");

这是XMLReader.inputFromjson(请求)

的代码
 public static InputDTO inputFromJson(HttpServletRequest request) throws IOException {
        InputDTO inputDTO = null;
        Gson gson = new Gson();

            logger.debug("before the fromJson call");
            inputDTO = gson.fromJson(request.getReader(), InputDTO.class);
            logger.debug("after the fromJson call");


        return inputDTO;

    }

失败的行是

pw.println(inputJson.getTimeZone());

InputDTO唯一的方法是

public String getTimeZone() {
    return timeZone;
}

public void setTimeZone(String timeZone) {
    this.timeZone = timeZone;
}

这是我发送的请求:

{"request":[
{"timeZone":"CST"}
]}

补充:堆栈跟踪

Week3 threw exception
java.lang.NullPointerException
at com.week3.Week3.doPost(Week3.java:136)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:643)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at com.ta.aa.dao.catalina.DAOValve.invoke(Unknown Source)
at com.ta.aa.catalina.realm.SingleSignOn.invoke(SingleSignOn.java:631)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:724)

如果你能提供帮助,请告诉我。

谢谢!

1 个答案:

答案 0 :(得分:0)

名称应该在json和类中匹配。您在json和类定义中发送的数据不匹配。改变你的班级定义。

class Request{

String tz;

public String gettz() {
    return tz;
}

public void settz(String tz) {
    this.tz= tz;
}

}
class InputDTO {

Request request;

public String getrequest() {
    return request;
}

public void setrequest(Request request) {
    this.request= request;
}

}