Java Can';无法从服务器方法获取JSONObject(Iternal服务器错误500)
我正在开发带有服务器端分页的JavaSpringBoot应用程序,我遇到了这个问题。当我从localhost:8080/tabelka获取所有数据时,一切都很好,但当我想从localhost:8080/pgTabelka获取数据时,postMAN会返回以下内容:Java Can';无法从服务器方法获取JSONObject(Iternal服务器错误500),java,json,spring-mvc,spring-boot,Java,Json,Spring Mvc,Spring Boot,我正在开发带有服务器端分页的JavaSpringBoot应用程序,我遇到了这个问题。当我从localhost:8080/tabelka获取所有数据时,一切都很好,但当我想从localhost:8080/pgTabelka获取数据时,postMAN会返回以下内容: { "timestamp": "2018-08-23T10:58:42.963+0000", "status": 500, "error": "Internal Server Error", "messa
{
"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;
}
}
现在邮递员回来了
{
"empty": false
}
编辑2:
我打印了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,为什么draw不是这个json对象的第一个呢?为什么邮递员会变得“空”?我假设您的请求中没有给出其中一个参数。例如,电话线 Integer.parseInt(request.getParameter(“draw”))
如果请求没有参数
draw
则引发NullPointerException
已初始化的HttpServletRequest有问题。
它应该作为方法参数传递,而不是在类级别初始化
公共JSONObject pgTable(HttpServletRequest)
关于响应中参数顺序的第二个问题的答案可以通过使用
@ResponseBy用户\定义\类getData()
在用户定义的类中,您可以使用
@XmlType(proporter=“”)
更多参考
@XmlType注释本身就足够了,不需要使用上面提到的示例2中的任何其他注释。首先,在获得如下参数之前,您应该拥有Request对象
@GetMapping("/pgTabelka")
public JSONObject pgTable(HttpServletRequest request)
{
}
在这一行设置断点JSONObject json=new JSONObject()代码>并逐行调试它
发生这种情况的原因是draw、start、length等参数之一为空
一旦您解决了NullPointerException,您可能还会收到NumberFormatException,以防止在转换为整数之前检查值是否可转换为整数。请确保在解析为整数之前,参数不应为null
@RequestMapping(value="/pgTabelka", method = {RequestMethod.GET})
@ResponseBody
public JSONObject pgTable(HttpServletRequest request)
{
//validate request parameter before converting it into Integer
}
NullPointerException应该指向文件和行号。检查文件和行号。如果有类似于someObject()的内容,则someObject为null。如果您有类似于someObject().callOnReturnedObject()的内容,那么someObject()很可能返回空值。您能提供更多信息吗?您通过邮递员发送的请求和服务日志中的完整堆栈跟踪将非常有用。@Michael yes。获取localhost:8080/pgTabelka并获取:{“timestamp”:“2018-08-23T11:47:37.039+0000”,“status”:500,“error”:“Internal Server error”,“message”:“null”,“path”:“/pgTabelka”}@Juan没有参数,您需要添加这些参数。至少draw
,start
和length
@Michael我想从数据表中获取draw、start和length等参数。但可能是我做错了……谢谢,你是对的,但现在对我来说一切都没有改变:/或者我错了。现在我有这样一个问题:“错误18272---[nio-8080-exec-2]o.a.c.c.c.[/].[dispatcherServlet]:路径为[]的上下文中Servlet[dispatcherServlet]的Servlet.service()引发异常[Request processing failed;嵌套异常为java.lang.NumberFormatException:null],根本原因是“对Request.getParameter执行空检查”(如果(request.getParameterName(“parameterName”)!=null{parseToINT……}我这样做了,现在邮递员正在返回“{”empty“:false}”返回“{”empty“:false}”“虽然名为json的变量不是空的,也不是空的,但有数据。是的,您是对的。参数可能是空的。但我不太明白如何修复它……请确保您的请求正文json包含所有3个字段和一些值。请查看编辑。看起来这3个字段不是空的,而是“空的”?谢谢你的回答。我已经编辑了我的帖子,字段看起来不是空的…@PawełMałysz你有没有检查你的返回JSON对象是否正确创建?(返回前将其设置为sysout)添加@ResponseBody并检查我添加它的方式:@ResponseBody@GetMapping(/pgTabelka)public JSONObject pgTable(HttpServletRequest){“还是一样,什么也没变
@RequestMapping(value="/pgTabelka", method = {RequestMethod.GET})
@ResponseBody
public JSONObject pgTable(HttpServletRequest request)
{
//validate request parameter before converting it into Integer
}