小编Reb*_*cca的帖子

在springboot中编码和解码参数的最佳方法是什么?

我用来@RequestParam获取参数值,但我发现如果我传递像“name=abc&def&id=123”这样的值,我将得到名称值“abc”而不是“abc&def”。我发现对参数值进行编码和解码可以解决我的问题。但是我必须在每个控制器方法中编写编码和解码方法,spring有解码每个 @RequestParam值的全局方法吗?使用时@RequestParam,是否需要编码和解码每个值?

这是我的代码:

@PostMapping("/getStudent")
public Student getStudent(
        @RequestParam String name,
        @RequestParam String id) { 
        name= URLDecoder.decode(name, "UTF-8");  
        //searchStudent
        return Student;
}

@PostMapping("/getTeacher")
public teacher getTeacher(
        @RequestParam String name,
        @RequestParam String teacherNo) { 
        name= URLDecoder.decode(name, "UTF-8");  
        //searchTeacher
        return teacher;
}
Run Code Online (Sandbox Code Playgroud)

有人说Spring已经这样做了,但是我尝试过,结果不对。只使用curl cmd可以,但是java代码不行。

@PostMapping(value = "/example")
public String handleUrlDecode1(@RequestParam String param) { 
    //print ello%26test
    System.out.println("/example?param received: " + param); 
    return "success";
}

@GetMapping(value = "/request")
public String request() {
    String url =  "http://127.0.0.1:8080/example?param=ello%26test";
    System.out.println(url);
    RestTemplate restTemplate = …
Run Code Online (Sandbox Code Playgroud)

java spring http-request-parameters spring-boot

10
推荐指数
1
解决办法
9629
查看次数

JDK8中可以只用一个流表达式来得到两个条件的结果吗?

这是我的代码,但我发现它不好。在JDK8中我可以只使用一个流表达式来获得两个条件的结果吗?我想确保id条件是第一个,名称条件是第二个。

  public Student getStudent(String id,String name)  {
    try {
        List<Student> students = studentDao.getStudent();
        if (CollectionUtils.isNotEmpty(students)) {
            Student studentDomain=students.stream()
                    .filter(p -> id .equals(p.getId()))
                    .findAny().orElse(null);
            if(studentDomain==null){
                studentDomain=students.stream()
                        .filter(p -> name.equals(p.getName()))
                        .findAny()
                        .orElse(null);
            }
            return studentDomain;
        } else {
            return null;
        }
    }catch (Exception ex){ 
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

java java-8 java-stream

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