Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在SpringMVC中从json获取参数?_Json_Spring_Servlet 3.0 - Fatal编程技术网

如何在SpringMVC中从json获取参数?

如何在SpringMVC中从json获取参数?,json,spring,servlet-3.0,Json,Spring,Servlet 3.0,我正在发送json数据: { "username":"abc@gmail.com", "password":"abc" } 在我的自定义过滤器中,我想访问用户名和密码 我的过滤器是: @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

我正在发送json数据:

{
    "username":"abc@gmail.com",
    "password":"abc"
}
在我的自定义过滤器中,我想访问用户名和密码

我的过滤器是:

@Override
public void doFilter(ServletRequest request, ServletResponse response, 
        FilterChain chain) throws IOException, ServletException {
    //code to get parameters from json
}

使用apache IO utils,从输入流获取JSON字符串,并将JSON字符串转换为映射:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-io</artifactId>
    <version>${commons-io.version}</version>
</dependency>

@Override

public void doFilter(ServletRequest request, ServletResponse
 response, FilterChain chain) throws IOException, ServletException {
        try {
            String jsonBody = IOUtils.toString(request.getInputStream());
            //convert json string to HashMap
            //using http://stackoverflow.com/a/22011887/1358551
        } catch (Exception e) {
            logger.warn("", e);
            return new ResponseEntity<String>(e.getMessage(),
                    HttpStatus.BAD_REQUEST);
        }
    }

我建议使用googlegson解析器

Map<String, String> extractLoginRequest(final ServletRequest request) throws IOException {
    final StringBuffer sb = new StringBuffer();
    String line = null;
    final BufferedReader reader = request.getReader();
    while ((line = reader.readLine()) != null) {
        sb.append(line);
    }
    // as far as I know, gson might not be 100% threadsave
    return new Gson().fromJson(sb.toString(), HashMap.class);
}
现在,您可以使用访问参数

Map<String, String> loginRequest = extractLoginRequest(request);
loginRequest.get("username") 
如果您使用的是Maven,则可能会使用以下依赖项:

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.3.1</version>
</dependency>

我尝试了request.getParameterusername,但不起作用。通过读取request.getInputStream或request.getReader,您只能将JSON作为字符串获取……您可以使用上述JSON数据向我发送示例吗