当我的请求具有所有值时,为什么我的GetParameter返回null?
我正在通过Axios向我的React应用程序发送一些数据,以通过Servlet在Eclipse上向后端发送。我的帖子请求具有正确的有效负载,即使我使用代码request.getReader()。lines()。collector.joining(collectors.joining(system.lineseparator()))
请求的全部内容。这是一种JSON格式:
{“ business_code”:“ 90”,“数字”:“ 90”,“ date”:“ 2022-01-10” ...
但是当我尝试从任何字段获取值时,它存储一个空字符串然后我的数字字段引发了例外。空字符串:
System.out.println("Bizz Code" + request.getParameter("business_code"));
bizz codenull
我不明白为什么一切实际上都在那里时会变得无效。为什么会发生这种情况?
I am sending some data from my react app through Axios to my backend on Eclipse through servlets. My POST request has the right payload and even when I inspect the request with the code request.getReader().lines().collect(Collectors.joining(System.lineSeparator()))
, I get the entire contents of the request. It's a JSON format:
{"business_code":"90","number":"90","date":"2022-01-10"...
But when I try to get the value from any of the fields, it stores a null string and my numerical fields then throw an exception. Null string:
System.out.println("Bizz Code" + request.getParameter("business_code"));
Bizz Codenull
I don't understand why I am getting null when everything is actually there. Why is this happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很好地查看
getReader()
和getParameter()
的文档。他们无法访问HTTP请求的相同部分 -getReader()
在身体而不是参数上操作。当您将请求的主体提取到字符串中时,您需要从字符串中解析JSON来做您要做的事情。通常,您会看到杰克逊(Jackson)或格森(Gson)用于此服务,但通常它们用于建造服务的框架,而不是原始的servlet。
Take a good look at the doc for
getReader()
andgetParameter()
. They do not access the same parts of an HTTP request--getReader()
operates on the body, not the parameters.While you have extracted the body of the request into a String, you'll need to parse the json from the string to do what you're trying to do. Often you'll see Jackson or GSON used for this, but typically they're used with a framework for building services, and not raw Servlets.