springmodelattributeget-mapping

Spring MVC @GetMapping @ModelAttribute percent(%) symbol gives null value


Here is my controller:

@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);
    }
}

And Here is my 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

Whenever there's percent(%) symbol in my GET request, it always give null value.


Solution

  • You should encode it.

    https://www.urlencoder.org/

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

    Your final url look like below

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