相关疑难解决方法(0)

如何从java中的HttpServletRequest检索原始发布数据

我正在尝试用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)

java post servlets http

55
推荐指数
2
解决办法
14万
查看次数

从HttpServletRequest检索JSON对象文字

我正在编写需要提取发布到servlet的对象文字的代码.我已经研究了HttpServletRequest对象的API,但是我不清楚如何从请求中获取JSON对象,因为它不是从网页上的表单元素发布的.

任何见解都表示赞赏.

谢谢.

post json servlets

49
推荐指数
6
解决办法
9万
查看次数

仅检索POST参数(Java)

有没有人知道从HttpServletRequest对象获取POST参数的方法?

IE,PHP有$ _POST超全局,而Perl的CGI.pm只会在HTTP方法为POST(默认情况下)时检索POST参数.

HttpServletRequest.getParameter(String)将包含 得到 即使HTTP方法是POST,也会显示URL参数.

java post servlets

12
推荐指数
2
解决办法
3万
查看次数

如何从Java Servlet中的HttpServletRequest获取文件

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)

而且我在想

  1. 是否有任何方法不使用第三方库从HttpServletRequest对象中获取文件?

  2. 什么request.getParameter("attachement")回报?是文件名还是其他什么?

  3. 二进制输入是由Web容器自动存储在文件系统中还是暂时存储在内存中?

java servlets

7
推荐指数
1
解决办法
5万
查看次数

SonarQube 6.7 LTS组权限API不起作用

我正在将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吗?

sonarqube sonarqube-api

5
推荐指数
1
解决办法
314
查看次数

如何以 JSON 格式发送整数值并在 REST Controller 中接收?

我有一个 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)

java spring json spring-boot spring-restcontroller

4
推荐指数
1
解决办法
2万
查看次数

dojo.xhrPost的参数

当我使用时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

dojo post get parameter-passing

3
推荐指数
1
解决办法
2万
查看次数

如何在Spring Boot Controller中读取帖子数据?

我想从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)

我曾尝试使用收集器作为替代方案,但这也不起作用.我究竟做错了什么?

spring spring-boot spring-restcontroller

2
推荐指数
1
解决办法
4070
查看次数