我正在尝试用Java获取发布数据.看起来它应该是最简单的事情之一吗?我的意思是,HttpServletRequest.getParameter必须做对吗?那么如何获得原始发布数据呢?
我发现HttpServletRequest获取JSON POST数据并使用Kdeveloper的代码从请求中提取发布数据.它有效,但有一个问题:我只能获得一次这样的帖子数据.
下面是我用Kdeveloper代码制作的方法:
public static String getPostData(HttpServletRequest req) {
StringBuilder sb = new StringBuilder();
try {
BufferedReader reader = req.getReader();
reader.mark(10000);
String line;
do {
line = reader.readLine();
sb.append(line).append("\n");
} while (line != null);
reader.reset();
// do NOT close the reader here, or you won't be able to get the post data twice
} catch(IOException e) {
logger.warn("getPostData couldn't.. get the post data", e); // This has happened if the request's reader is closed
} …Run Code Online (Sandbox Code Playgroud) 我正在编写需要提取发布到servlet的对象文字的代码.我已经研究了HttpServletRequest对象的API,但是我不清楚如何从请求中获取JSON对象,因为它不是从网页上的表单元素发布的.
任何见解都表示赞赏.
谢谢.
有没有人知道从HttpServletRequest对象获取POST参数的方法?
IE,PHP有$ _POST超全局,而Perl的CGI.pm只会在HTTP方法为POST(默认情况下)时检索POST参数.
HttpServletRequest.getParameter(String)将包含 得到 即使HTTP方法是POST,也会显示URL参数.
HTML中的表单就像
...
<form method="post" action="/foobar">
<input type="file" name="attachment" />
<input type="text" name="foo" />
... other input fields
</form>
Run Code Online (Sandbox Code Playgroud)
而Servlet就像
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String attachment = request.getParameter("attachement");
String foo = request.getParameter("foo");
// get other parameters from the request
// and get the attachment file
}
Run Code Online (Sandbox Code Playgroud)
而且我在想
是否有任何方法不使用第三方库从HttpServletRequest对象中获取文件?
什么request.getParameter("attachement")回报?是文件名还是其他什么?
二进制输入是由Web容器自动存储在文件系统中还是暂时存储在内存中?
我正在将SonarQube从5.6版迁移到6.7版.我正在使用SonarQube API和我的Jenkins作业,问题是群组权限的API不适用于6.7版本...
我已尝试手动使用Postman(POST原始JSON):
{
"groupName": "project-name-admin",
"permission": "admin",
"projectKey": "project-name"
}
Run Code Online (Sandbox Code Playgroud)
返回的结果是:
{
"errors": [
{
"msg": "Group name or group id must be provided"
}
]
}
Run Code Online (Sandbox Code Playgroud)
如果我使用它也是一样的:
{
"groupId": 53,
"permission": "admin",
"projectKey": "project-name"
}
Run Code Online (Sandbox Code Playgroud)
要么
{
"groupId": 53,
"groupName": "project-name-admin",
"permission": "admin",
"projectKey": "project-name"
}
Run Code Online (Sandbox Code Playgroud)
它与6.5 verison一起工作,我不知道这个问题可能来自哪里:(
@SonarQube开发团队:你能修好thaaaat吗?
我有一个 REST 控制器,我在其中编写了此代码
@PostMapping(value = "/otp")
public void otp(@RequestBody Integer mobile) {
System.out.println(" Mobile = "+mobile);
}
Run Code Online (Sandbox Code Playgroud)
我使用以下输入从 Postman 调用此方法
URL : localhost:8080/otp
Body :
{
"mobile":123456
}
Run Code Online (Sandbox Code Playgroud)
但我收到以下异常
org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not deserialize instance of java.lang.Integer out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.Integer out of START_OBJECT token
Run Code Online (Sandbox Code Playgroud)
如果我将 String 作为这样的参数
@PostMapping(value = "/otp")
public void otp(@RequestBody String mobile) {
System.out.println(" Mobile = "+mobile);
}
Run Code Online (Sandbox Code Playgroud)
并将输入作为
{
"mobile":123456
}
Run Code Online (Sandbox Code Playgroud)
现在它在控制台中打印如下
Mobile …Run Code Online (Sandbox Code Playgroud) 当我使用时dojo.xhrGet,我用这种方式通过它发送多个参数GET
dojo.xhrGet
({
url:"MyServerPageURL?Param_A="+"ValueA"+"&Param_2="+"Value2",
load: function(data)
{
//do something
},
preventCache: true,
sync: true,
error: function(err)
{
alert("error="+err);
}
});
Run Code Online (Sandbox Code Playgroud)
当我不得不使用它时,我怎么能做类似的事情(发送多个参数)dojo.xhrPost?
我想从Spring Boot控制器读取POST数据.
我已经尝试了这里给出的所有解决方案:HttpServletRequest获取JSON POST数据,但我仍然无法读取Spring Boot servlet中的post数据.
我的代码在这里:
package com.testmockmvc.testrequest.controller;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
@Controller
public class TestRequestController {
@RequestMapping(path = "/testrequest")
@ResponseBody
public String testGetRequest(HttpServletRequest request) throws IOException {
final byte[] requestContent;
requestContent = IOUtils.toByteArray(request.getReader());
return new String(requestContent, StandardCharsets.UTF_8);
}
}
Run Code Online (Sandbox Code Playgroud)
我曾尝试使用收集器作为替代方案,但这也不起作用.我究竟做错了什么?