无法从服务器方法获取JSONObject(内部服务器错误500)

时间:2018-08-23 11:10:22

标签: java json spring-mvc spring-boot

我正在使用服务器端分页来开发Java Spring Boot应用程序,但遇到了问题。当我从localhost:8080 / tabelka获取所有数据时,一切都很好,但是当我想从localhost:8080 / pgTabelka获取数据时,postMAN返回此:

{
    "timestamp": "2018-08-23T10:58:42.963+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "No message available",
    "path": "/pgTabelka"
}

我的控制器:

package com.example.controller;


import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.dao.SeriaDao;
import com.example.model.Seria;


@RestController
public class WebController {

    @Autowired
    SeriaDao sed;

    HttpServletRequest request;


    @GetMapping("/tabelka")
    public List<Seria> showTable()
    {
        return sed.findAll();
    }

    @GetMapping("/pgTabelka")
    public JSONObject pgTable()
    {
        JSONObject json = new JSONObject();
        int draw = Integer.parseInt(request.getParameter("draw"));
        int start = Integer.parseInt(request.getParameter("start"));
        int length = Integer.parseInt(request.getParameter("length"));
        int totalRecords = sed.recordsTotal();
        List<Seria> serie = sed.findPart(start, length);

        try {
            json.put("draw", ++draw);
            json.put("recordsTotal", totalRecords);
            json.put("recordsFiltered", totalRecords);
            json.put("data", serie);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return json;
    }
}

也许您可以帮助我发现错误。我在应用程序中的控制台是returnint“错误14496-[nio-8080-exec-5] oaccC [。[。[/]。[dispatcherServlet]:Servlet [dispatcherServlet]的Servlet.service(),路径为[]引发了异常[请求处理失败;嵌套的异常是java.lang.NullPointerException]并有根本原因”,但是我不确定这是否与我编写的内容有关。感谢您的帮助!

编辑: 我更改了这样的代码,但控制台中没有错误

int draw = 0;
        int start = 0;
        int length = 10;

        JSONObject json = new JSONObject();
        if(request.getParameter("draw")!=null)
            draw = Integer.parseInt(request.getParameter("draw"));
        if(request.getParameter("start")!=null)
            start = Integer.parseInt(request.getParameter("start"));
        if(request.getParameter("length")!=null)
            length = Integer.parseInt(request.getParameter("length"));

现在POSTman返回

{
    "empty": false
}

EDIT2: 我打印了json变量,我得到了:

{"recordsFiltered":488,"data":[{"feed":"Macroeconomic_And_Major_Markets","unit":"$ per Week","file":"SMU48185800500000011.csv","cassid":"1d2e556b-031e-4c6f-aec4-981c4e907324","name":"Average Weekly Earnings of All Employees: Total Private in Corpus Christi, TX (MSA)","createdate":"2016-10-15T23:38:49","changedate":"2017-01-09T00:50:01","id":41,"datefrom":"2006-12-31","categoryid":3,"frequency":5},{"feed":"Macroeconomic_And_Major_Markets","unit":"Index 2015=100","file":"CP0910CYM086NEST.csv","cassid":"6df5b8d5-ec39-4860-930f-a8b355cce37a","name":"Harmonized Index of Consumer Prices: Audio-Visual, Photographic, and Information Processing Equipment for Cyprus","createdate":"2016-10-15T23:38:49","changedate":"2017-02-16T11:35:04","id":42,"datefrom":"1995-12-31","categoryid":3,"frequency":5},{"feed":"Macroeconomic_And_Major_Markets","unit":"$ per Week","file":"SMU48185800500000011A.csv","cassid":"c3bad2a6-7ad5-4091-846b-f84e9b9f7b50","name":"Average Weekly Earnings of All Employees: Total Private in Corpus Christi, TX (MSA)","createdate":"2016-10-15T23:38:49","changedate":"2017-02-16T11:32:01","id":43,"datefrom":"2006-12-31","categoryid":3,"frequency":8},{"feed":"Macroeconomic_And_Major_Markets","unit":"Index 2015=100","file":"CP0910CZM086NEST.csv","cassid":"84483d95-d914-45ab-a87a-8c063bfc6d0f","name":"Harmonized Index of Consumer Prices: Audio-Visual, Photographic, and Information Processing Equipment for Czech Republic","createdate":"2016-10-15T23:38:49","changedate":"2017-02-16T11:35:04","id":44,"datefrom":"1999-11-30","categoryid":3,"frequency":5},{"feed":"Macroeconomic_And_Major_Markets","unit":"$ per Week","file":"SMU41187000500000011SA.csv","cassid":"285056e3-94ce-4266-98b4-ddf32602c0c2","name":"Average Weekly Earnings of All Employees: Total Private in Corvallis, OR (MSA)","createdate":"2016-10-15T23:38:49","changedate":"2017-01-09T00:50:01","id":45,"datefrom":"2006-12-31","categoryid":3,"frequency":5},{"feed":"Macroeconomic_And_Major_Markets","unit":"Index 2015=100","file":"CP0910DKM086NEST.csv","cassid":"9798eadf-1503-48a5-91e3-cad5852db745","name":"Harmonized Index of Consumer Prices: Audio-Visual, Photographic, and Information Processing Equipment for Denmark","createdate":"2016-10-15T23:38:49","changedate":"2017-02-16T11:35:04","id":46,"datefrom":"1995-12-31","categoryid":3,"frequency":5},{"feed":"Macroeconomic_And_Major_Markets","unit":"$ per Week","file":"SMU41187000500000011.csv","cassid":"d4b22376-2f84-404f-a175-c04ad8ced5a5","name":"Average Weekly Earnings of All Employees: Total Private in Corvallis, OR (MSA)","createdate":"2016-10-15T23:38:49","changedate":"2017-01-09T00:50:01","id":47,"datefrom":"2006-12-31","categoryid":3,"frequency":5},{"feed":"Macroeconomic_And_Major_Markets","unit":"Index 2015=100","file":"CP0910EEM086NEST.csv","cassid":"72880ba0-722e-46bb-98f4-7d388d217c26","name":"Harmonized Index of Consumer Prices: Audio-Visual, Photographic, and Information Processing Equipment for Estonia","createdate":"2016-10-15T23:38:49","changedate":"2017-02-16T11:35:04","id":48,"datefrom":"1995-12-31","categoryid":3,"frequency":5},{"feed":"Macroeconomic_And_Major_Markets","unit":"$ per Week","file":"SMU41187000500000011A.csv","cassid":"03fdf50e-9cd9-483b-aecf-1a0a9e93fa4d","name":"Average Weekly Earnings of All Employees: Total Private in Corvallis, OR (MSA)","createdate":"2016-10-15T23:38:49","changedate":"2017-02-16T11:32:01","id":49,"datefrom":"2006-12-31","categoryid":3,"frequency":8},{"feed":"Macroeconomic_And_Major_Markets","unit":"Index 2005=100","file":"CP0910EZ17M086NEST.csv","cassid":"2943c0f2-ad55-4ec4-98d0-70db25deaefc","name":"Harmonized Index of Consumer Prices: Audio-Visual, Photographic, and Information Processing Equipment for Euro area (17 countries) (DISCONTINUED)","createdate":"2016-10-15T23:38:49","changedate":"2017-02-16T11:35:04","id":50,"datefrom":"1995-12-31","categoryid":3,"frequency":5}],"draw":1,"recordsTotal":488}

所以看起来一切都很好,但是如果我先添加draw,为什么不首先在此json对象中绘制?为何POSTman变得“空”?

4 个答案:

答案 0 :(得分:1)

您初始化的HttpServletRequest存在问题。 应该将其作为方法参数传递,而不是在类级别进行初始化。

  

公共JSONObject pgTable(HttpServletRequest请求)

您可以通过使用来解决关于参数响应顺序的第二个问题的答案

  

@ResponseBody USER_DEFINED_CLASS getData()

在USER_DEFINED_CLASS内部,您可以使用

  

@XmlType(proporder =“”)

更多参考

  1. @ResponseBody:click here
  2. @XmlType:click here

@XmlType注释本身就足够了,无需使用上述示例2中的任何其他注释

答案 1 :(得分:0)

我假设您的请求中未提供参数之一。例如。线

Integer.parseInt(request.getParameter(“ draw”))

如果请求中没有参数NullPointerException,则会抛出draw

答案 2 :(得分:0)

首先,在获取如下所示的参数之前,您应该具有Request Object。

@GetMapping("/pgTabelka")
    public JSONObject pgTable(HttpServletRequest request)
    {

}

在此行JSONObject json = new JSONObject();上放置一个断点,并逐行调试它。

之所以发生这种情况,是因为参数draw,start,length之一为空。

一旦解决了NullPointerException异常,您也可能会获得NumberFormatException异常,以防止在转换为整数之前检查这些值是否可转换为整数。

答案 3 :(得分:0)

请确保在解析为Integer参数之前不应为空

    @RequestMapping(value="/pgTabelka", method = {RequestMethod.GET})
    @ResponseBody
    public JSONObject pgTable(HttpServletRequest request)
        {
           //validate request parameter before converting it into Integer
    }