Getting object from @PathParam

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 06:38:26

问题


I'm creating a RESTful service for accessing data.

So I started writing that service, first I created a ReadOnlyResource interface with the following code:

public interface ReadOnlyResource<E, K> {
    Collection<E> getAll();
    E getById(K id);
}

Where E is the returned type and K is the key element.

So if I'm implementing with <Integer, Integer> I'll inject the key like t

@GET
@Path("/{id}")
@Override
public Integer getById(@PathParam("id") Integer id) {
    return null;
}

But when my key is more complex, like this:

public class ComplexKey {
    private String name;
    private int value;
}

How can I inject this so I can use my interface?

Is there a way to inject both params and create the key with them?

EDIT: the solution of @QueryParam doesn't help because what I try to reach is going to /some name/some number and receive a ComplexKey instance which contains the some name and some number values from the url.


回答1:


what I try to reach is going to /some name/some number and receive a ComplexKey instance which contains the some name and some number values from the url

Use a @BeanParam

public class ComplexKey {
    @PathParam("name")
    private String name;
    @PathParam("value")
    private int value;
    // getters/setters
}

@Path("/{name}/{value}")
getById(@BeanParam ComplexKey key)


来源:https://stackoverflow.com/questions/43178754/getting-object-from-pathparam

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