【问题标题】:Spring MVC @GetMapping @ModelAttribute percent(%) symbol gives null valueSpring MVC @GetMapping @ModelAttribute percent(%) 符号给出空值
【发布时间】:2021-04-22 13:16:10
【问题描述】:

这是我的控制器:

@CrossOrigin(origins = "http://localhost:3000")
@RestController
@RequestMapping("/api/v1/employees")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @GetMapping()
    public List<Employee> getEmployeesBySearch(@Valid @ModelAttribute SearchDto searchDto) {
        return employeeService.getEmployeesBySearch(searchDto);
    }
}

这是我的 SearchDto:

public class SearchDto {
    
    private String firstName;

    public String getFirstName() {
        return this.firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
}

.

http://localhost:8080/api/v1/employees?firstName=%%%
http://localhost:8080/api/v1/employees?firstName=a%
http://localhost:8080/api/v1/employees?firstName=%a

每当我的 GET 请求中有百分比 (%) 符号时,它总是给出空值。

【问题讨论】:

  • 您需要对%符号进行html编码。

标签: spring modelattribute get-mapping


【解决方案1】:

你应该对其进行编码。

https://www.urlencoder.org/

a%  ->  a%25
%%% ->  %25%25%25
name%surname -> name%25surname

您的最终网址如下所示

http://localhost:8080/api/v1/employees?firstName=a%25

【讨论】:

    猜你喜欢
    • 2018-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 1970-01-01
    相关资源
    最近更新 更多