카테고리 없음

Spring MVC @GetMapping @ModelAttribute percent (%) 기호는 null 값을 제공합니다.

기록만이살길 2021. 3. 1. 02:05
반응형

Spring MVC @GetMapping @ModelAttribute percent (%) 기호는 null 값을 제공합니다.

1. 질문(문제점):

내 컨트롤러는 다음과 같습니다.

@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 요청에 백분율 (%) 기호가있을 때마다 항상 null 값을 제공합니다.

2. 해결방안:

인코딩해야합니다.

https://www.urlencoder.org/

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

최종 URL은 다음과 같습니다.

http://localhost:8080/api/v1/employees?firstName=a%25
65771169
반응형