1
votes

https://drive.google.com/file/d/1OiFUCKh1M5Su73SD7Xp8Y_LDqGpyVqC3/view?usp=sharing

I am working with REST api using jersey and I send raw json with postman like this. My code

@POST
    @Consumes({MediaType.APPLICATION_JSON})
    @Path("register")
    public Response register(@QueryParam("email") String email, @QueryParam("username") String userName, @QueryParam("password") String password) {
        System.out.println("username: " + userName);
        System.out.println("password: " + password);
        System.out.println("email: " + email);
}

But I receive

//Output using raw json
username: null,
password: null,
email: null

and I dont know why. If I use Param (the button to the left of Send button in Postman and set key-value) not Raw json. It works

//Output when use param
username: abc,
password: 123456
email: email

Note that: I did set Header with Content-Type: application/json

1
You are using QueryParam but sending the data as body. You need to either pass the data as part of query parameters or change register method signature to accept a pojo that will have username, password and email fields in it. - tsolakp
Beacause I intend to send json array (example {"email":["a", "b"]}) and array cannoy be passed as query param. How can I do that - coinhndp

1 Answers

0
votes

As I mentioned in my comment, you need to either pass the data as part of query parameters or change register method signature to accept a pojo that will have username, password and email fields in it

Change your method to something like this:

@POST
@Consumes({MediaType.APPLICATION_JSON})
@Path("register")
public Response register(User user) {
    System.out.println("username: " + user.getUsername() );
    System.out.println("password: " + user.getPassword() );
    System.out.println("email: " + user.getEmail() );
}

public class User{
    private String username = null;
    private String password= null;
    private String email = null;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    } 
}