glass fish java can i send object through get request?

久未见 提交于 2019-12-25 18:42:36

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/33174590/glass-fish-java-can-i-send-object-through-get-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!