Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a class like this:

class Customer {
    private int id;
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

and I have a glass fish web service:

i want to know it is possible to send a customer object using get

(i know i can do this in post, but in get ... i don't know)

this is what i tried:

@GET
    @Path("/test")
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes(MediaType.APPLICATION_OCTET_STREAM)
    public String test(@QueryParam("customer") Customer customer) {
        return "Done " + customer.getId();
    }

then i call it like this:

..../test?id=4&name=william

I know that is wrong, but i don't know the correct way, and i don't know if that is even possible using get

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
192 views
Welcome To Ask or Share your Answers For Others

1 Answer

@QueryParam should be used for each individual parameter. For instance

/cusomters?name=hello&id=1

@GET
@Produces(...)
public Response get(@QueryParam("name") String name,
                    @QueryParam("id") int id)

If you want put it into a bean, you can use @BeanParam, which allows you to put arbitrary @XxxParams into a bean. For example

class Customer {
    @QueryParam("name")
    private String name;
    @QueryParam("id")
    private int id;
    // getters/setters
}

@GET
public Response get(@BeanParam Customer customer)

But do keep in mind REST principles. To create a customer resource, it should be done with POST. Also be considerate of security concerns. You do not want private user information in URLs.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...