在curl GET请求中,当有多个请求参数时出现空指针异常。

huangapple go评论53阅读模式
英文:

Getting null pointer exception on more than one request parameter in curl GET request

问题

1, 单个请求参数的Curl命令:

curl --insecure -X GET "Content-Type:application/json" https://localhost/test?pageSize=1
正常运行。

2, 多个请求参数的Curl命令:

curl --insecure -X GET "Content-Type:application/json" https://localhost/test?pageSize=1&pageNumber=0
我得到pageNumber是null,抛出空指针异常。

示例Java控制器类

@Controller
@RequestMapping("/api/test")
public class TestController {
    @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Object> getTest(
        @RequestParam(value = "pageSize", required = false) Integer pageSize,
        @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
        HttpServletRequest hsrequest, HttpServletResponse hsresponse) {
    // 实现部分
}
英文:

1, Curl command for single request parameter:

curl --insecure -X GET &quot;Content-Type:application/json&quot; https://localhost/test?pageSize=1
Working fine.

2, Curl command for mulitple request parameters:

curl --insecure -X GET &quot;Content-Type:application/json&quot; https://localhost/test?pageSize=1&amp;pageNumber=0
I'm getting pageNumber is null, throwing null pointer exception

Sample Java controller class

@Controller
    @RequestMapping(&quot;/api/test&quot;)
    public class TestController {
    @RequestMapping(method = RequestMethod.GET, producesMediaType.APPLICATION_JSON_VALUE)
    	public ResponseEntity&lt;Object&gt; getTest(
    			@RequestParam(value = &quot;pageSize&quot;, required = false) Integer pageSize,
    			@RequestParam(value = &quot;pageNumber&quot;, required = false) Integer pageNumbe,
    			HttpServletRequest hsrequest, HttpServletResponse hsresponse) {
    //implementation
    }

答案1

得分: -1

curl --insecure -X GET "Content-Type:application/json" "https://localhost/test?pageSize=1&pageNumber=0"

英文:

2, Curl command for mulitple request parameters:

curl --insecure -X GET "Content-Type:application/json" https://localhost/test?pageSize=1&pageNumber=0

Solution: Added quotes to URL

curl --insecure -X GET "Content-Type:application/json" "https://localhost/test?pageSize=1&pageNumber=0"

huangapple
  • 本文由 发表于 2020年1月6日 17:20:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609449.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定