1

jersey、jax-rsを使用してWebサービスアプリケーションを構築しています

パス「/authenticate」に単一のjax-rsリソースファイルがあります

「/user」「/test」のような個別のパスを持つ複数のメソッドがあります

@Path ("/authenticate")
public class Authenticate{
private static final Log log = LogFactory.getLog(Authenticate.class);

@QueryParam("entityId")
String entity;

@GET
@Path ("/{param}")
public Response getMsg(@PathParam ("param") String msg) {
    String o = "Hello Welcome Back:"+msg;
    return Response.status(200).entity(o).build();
}

@GET
@Path ("/user")
@Produces({"application/json"})
public UserDTO getUser (@Context HttpServletRequest request,
        @QueryParam("userId") int userId) {
    System.out.println("In Get User, User:"+userId);    
    System.out.println("In Get User, Entity:"+entity);
}

@GET
@Path ("/test")
@Produces({"application/json"})
public TestPOJO getTestPOJO () {
    System.out.println("In Get TestPOJO");
    System.out.println("In Get Test, Entity:"+entity);
    return new TestPOJO();
}

}

ジャージクライアントに提案されているように、私はクライアントからの単一のWebリソースを使用しており、.path( "/ xxx")を使用して同じWebリソースから後続のWebリソースを構築しています。

これが私が最初のウェブリソースを作成する方法です

WebResource webResource = client.resource("http://localhost:8080/Service/jaxrs/authenticate");
webResource.queryParam("entityId", securityHelper.getEntityId().toString());

その後、Webリソースを使用する方法は次のとおりです

MultivaluedMap<String, String> params = new MultivaluedMapImpl();           
ClientResponse userRes = webResource.path("/user").queryParams(params).accept("application/json").get(ClientResponse.class);

最初のWebリソースにqueryparamを割り当て、.path()を使用して作成された後続のすべてのWebリソースによって保持されるようにします。しかし、それは今は起こっていません。たとえば、上記のコードでは、path( "/ user")を使用して呼び出しを行った場合、"entityId"は使用できません。

私の考えは、共通のパラメーターを一度割り当てることであり、webResourceの後続のすべてのユーザーはそれらを何度も追加する必要はありません。それを行う方法はありますか?このアプローチは機能しますか?

4

2 に答える 2

1

次の行は、新しいWebResourceを作成し、webResourceオブジェクトの状態を変更しません。

webResource.queryParam("entityId", securityHelper.getEntityId().toString())

最終的には、次のようにコードを変更して「ベース」リソースを作成できます。

WebResource webResource = client.resource("http://localhost:8080/Service/jaxrs/authenticate").queryParam("entityId", securityHelper.getEntityId().toString());

次に、このリソースを使用して、必要に応じて別のリソースを作成します。WebResource.queryParamおよびWebResource.queryParamsは、常に新しいWebResourceオブジェクトを作成します。

于 2012-08-14T21:27:21.037 に答える
0

I'm may not be the best person to answer this since I have entered the "world" of Jersey and RESTful servers not too long ago but since i saw this unanswered for 2 days ill try to help out as best as I can.

If i understood correctly you are trying to, by using a query, save the user information on entityId String so it will be available when you make a subsequent call.

Ok let's start with what you have. With your code (entityId as a global parameter), what you are specifying , is that when you are calling a resource from the Authenticate class, any call can be made with a query of the type '?entityId="something" and ANY method in this class can use the information sent in the query.

The thing is, for what I've learned by messing about with Jersey, whenever you make a call, the resource class (in your case Authenticate) is instantiated again. Therefor you can't just keep information in a global parameter since subsequent calls will have the String entityId as null.

This means that if you want to save the information you'll have to do it in a external resource (ex: db, file, etc). What method you choose depends on what you want to do and what are you looking for in your application.

I hope I was at least able to shed a sliver of light on your problem.

于 2012-08-14T17:35:28.007 に答える