在Jersey +一般设计问题中使用其他参数发布/放置/删除http Json [英] Post/Put/Delete http Json with additional parameters in Jersey + general design issues

查看:134
本文介绍了在Jersey +一般设计问题中使用其他参数发布/放置/删除http Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,我没有找到任何正常的方法来执行以下操作:

For some reason, I haven't found any normal way to do the following:

我想发布一个json对象,并为调用添加其他参数(在这种情况下,身份验证令牌)。
这是myUrl / server中的一个简单RESTful服务器,它可以访问url myUrl / server / person / personCode / resourceName中person的不同资源。

I want to Post a json object, and add additional parameters to the call (in this case, an authentication token). This is a simple RESTful server in myUrl/server, which should give access to different resources of a "person" in the url myUrl/server/person/personCode/resourceName.

GET很简单,不需要任何对象,只需要参数。
当我开始POST时问题就出现了 - 如何附加JSON,并保留其他参数?

GET is easy, and requires no object, only parameters. The problem arrises when I get to POST - how do I attach the JSON, and keep the other parameters as well?

该类(已被删除)为清晰起见和专有理由...):

The class (much has been removed for clarity and proprietary reasons...):

//Handles the person's resources
@Path("/person/{personCode}/{resourceName}")
public class PersonResourceProvider {

@GET
@Produces("application/json")
public String getPersonResource(@PathParam("personCode") String personCode, @PathParam("resourceName") String resourceName, @DefaultValue("") @QueryParam("auth_token") String auth_token) throws UnhandledResourceException, UnauthorizedAccessException {

    //Authenticates the user in some way, throwing an exception when needed...
    authenticate(personCode, auth_token, resourceName);

    //Returns the resource somehow...
}

@POST
@Produces("application/json")
public String postPersonResource(@PathParam("personCode") String personCode, @PathParam("resourceName") String resourceName, @DefaultValue("") @QueryParam("resourceData") String resourceData, @DefaultValue("") @QueryParam("auth_token") String auth_token) throws UnhandledResourceException, UnauthorizedAccessException {

    //Again, authenticating
    authenticate(personCode, auth_token, resourceName);

    //Post the given resource
    }
}

现在,GET方法非常有效,当你转到
myUrl / person / personCode / resourceName时,它会给我正确的资源。
auth_token用于每次调用服务器(现在,通过与预定义的字符串进行比较来完成身份验证),因此需要它。所有其他参数都是通过路径提供的,除了身份验证令牌之外,它不应该在路径中,因为它与所需资源的身份无关。

Now, the GET method works perfectly, when you go to myUrl/person/personCode/resourceName, it gives me the correct resource. The auth_token is used with every single call to the server (for now, authentication is done by comparing with a predefined string), so it's needed. All the other parameters are provided through the path, except for the authentication token, which should not be in the path as it does not relate to the identity of the required resource.

当我进入POST时,这是一个问题。
我知道有一种方法可以告诉它消耗JSON的方法,但在这种情况下,其他参数会发生什么(auth_token就是其中之一)?
我应该使用Multipart吗?

When I get to POST, it's a problem. I know there's a way to tell the method it consumes a JSON, but in that case, what will happen to the other parameters (auth_token is one of them)? Should I use Multipart?

另一个相关的问题,这是我第一次设计这样的服务器,这个设计是否正确?

Another related question, this is the first time I've designed such a server, is this design correct?

谢谢!

推荐答案

找到了!

客户端:

Client c = Client.create();
WebResource service = c.resource("www.yourserver.com/");
String s = service.path("test/personCode/resourceName")
                .queryParam("auth_token", "auth")
                .type("text/plain")
                .post(String.class, jsonString);

服务器端:

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;

@Path("/test/{personCode}/{resourceName}")
public class TestResourceProvider {

@POST
@Consumes("text/plain")
@Produces("application/json")
public String postUserResource(String jsonString,                                              
                               @PathParam("personCode") String personCode,                                                                                      
                               @PathParam("resourceName") String resourceName,                                                   
                               @QueryParam("auth_token") String auth_token)                                                  
                               throws UnhandledResourceException {

    //Do whatever...    

    }
}

在我的情况下,我将解析我在服务器中获得的json取决于资源名称,但您也可以传递对象本身,并使服务器使用application / json。

In my case, I will parse the json I get in the server depending on the resource name, but you can also pass the object itself, and make the server consume an "application/json".

这篇关于在Jersey +一般设计问题中使用其他参数发布/放置/删除http Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆