Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

glassfish - glass fish java can i send object through get request?

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

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

1 Reply

0 votes
by (71.8m points)

@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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...