I have a rest API (PUT verb) which accepts both request body and path params:
Ex:
curl --data {a:1, b:2} -X PUT "https://example.com/users/{username}/address/{addressname}"
I am trying to fetch both request body and path param in one POJO
Response myAPI(@BeanParam Users user){
system.out.println(user.username);
system.out.println(user.a);
Users class
public class Users{
@PathParam(username)
private String userName;
......
private String a;
......
}
But I am getting value of user.a as null. How to parse both request body and param in same class?
You can do this with a custom annotation and an
InjectionResolver. What theInjectionResolverdoes is allow you to create a custom injection point with your own annotation. So you could do something likeWhen implementing the
InjectionResolver, you would grab the actual body from theContainerRequestusing theContainerRequest#readEntity(Class)method. You would determine theClassto pass by doing some reflection on theFieldthat you can obtain inside theInjectionResolver. Below is a complete example using Jersey Test Framework. Run it like any other JUnit test.