I am trying to configure an Interface with some REST methods with Spring Boot. But when I implement that interface on a concrete class, the @PathVariable or @RequestParam do not work properly unless I repeat the configuration on my concrete method (which I dont want because the interface is being generated by a maven plugin). Lets take a look at the interface and the concrete class.
@RequestMapping("employees")
public interface EmployeesResource {
@RequestMapping(method = RequestMethod.GET, produces = {"application/json"})
EmployeesResource.GetEmployeesResponse getEmployees(
@RequestParam(value = "pageNum", defaultValue = "0")
long pageNum,
@RequestParam(value = "pageSize", defaultValue = "10")
long pageSize)
throws Exception;
@RequestMapping(method = RequestMethod.GET, value = "{employeeId}", produces = {
"application/json"
})
EmployeesResource.GetEmployeesByEmployeeIdResponse getEmployeesByEmployeeId(
@PathVariable("employeeId")
long employeeId)
throws Exception;
}
Now lets take a look at the concrete class.
@RestController
public class EmployeesResourceImpl implements EmployeesResource {
@Override
public EmployeesResource.GetEmployeesByEmployeeIdResponse getEmployeesByEmployeeId(long employeeId) {
//omitted
return EmployeesResource.GetEmployeesByEmployeeIdResponse.withJsonOK(e);
}
@Override
public EmployeesResource.GetEmployeesResponse getEmployees(long pageNum, long pageSize)
throws Exception {
//omitted
return EmployeesResource.GetEmployeesResponse.withJsonOK(list);
}
}
When I make a call to http://127.0.0.1:8080/employees/1 it throws an exception.
java.lang.IllegalStateException: Optional long parameter 'employeeId' is present but cannot be translated into a null value due to being declared as a primitive type. Consider declaring it as object wrapper for the corresponding primitive type.
I know that this is because me employeeId is long and not Long, but that is not the question because if I add the @PathVariable("employeeId") to the method on the concrete class, it all works.
So, what I want is to not repeat the configuration from the interface into the concrete class. Same goes for the @RequestParam(value = "pageNum", defaultValue = "0").
I have googled a lot and did not found that capacity of inheritance of annotations inside Spring Boot. Any help would be appreciated.
EDIT: Andy Wilkinson suggested this post Spring MVC Annotated Controller Interface but the guy concluded that there is no way around this due to AOP injections that Spring does. Is there really nothing to do here?