使用Jersey在Rest Api中传递两个对象 [英] Passing Two Objects in Rest Api using Jersey

查看:112
本文介绍了使用Jersey在Rest Api中传递两个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个Restful Web服务API,该API接受两个不同的对象,是否可以使用Jersey客户端调用此api.我无法使用Jersey客户端调用此功能. 这是Rest API的限制,我们不能将多个对象传递给一个方法.

I have written a Restful web service API, which is accepting two different Object, Is it possible to call this api using Jersey client. I am not able to call this using Jersey client. Is this a limitation of Rest API that we can not pass multiple objects to a method.

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/hello")
public class TimePassService {

    @POST
    @Path("/post")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response saveEmployeeInfo(final Employee input,final Manager input1) {
        String result = "Employee saved : " + input;

        System.out.println(input);
        System.out.println(input1);
        return Response.status(201).entity(result).build();
    }

}

当我与一些技术人员讨论此问题时,他们回答说不可能.解决方案是将这两个对象包装到第三个对象中,然后传递一个对象.

When I discussed this with some techies, they replied that it is not possible, The solution is to wrap these two object into a third object and then pass a single Object.

请告诉我是否还有其他解决方案.

Please let me know if there is some other solution of this.

推荐答案

这是不可能的.请参见 JAX-RS规范:

That is not possible. See the JAX-RS specification:

3.3.2.1实体参数

3.3.2.1 Entity Parameters

未用@FormParam注释的参数值或第3.2节中列出的任何注释(称为实体参数)均从请求实体主体映射.实体提供者和实体类型之间的转换是实体提供者的责任,请参见第4.2节.资源方法必须最多具有一个实体参数.

The value of a parameter not annotated with @FormParam or any of the annotations listed in in Section 3.2, called the entity parameter, is mapped from the request entity body. Conversion between an entity body and a Java type is the responsibility of an entity provider, see Section 4.2. Resource methods MUST have at most one entity parameter.

只能有一个 方法实体参数".

There can be only one method 'entity parameter'.

您要求的不是RESTful. REST不是RPC(远程过程调用),您不会将对象传递"到方法".在REST中,您可以从标识URL到标识URL传输资源表示形式.

What you ask for would not be RESTful. REST ist not RPC (Remote Procedure Call), you don't 'pass' objects to a 'method'. In REST you transfer Resource representations from and to identifying URLs.

在您的示例中,资源将是EmployeeInfo包裹EmployeeManager.

In your example the Resource would be an EmployeeInfo wrapping Employee and Manager.

此外,/post并不是一个非常RESTful的URL.由此识别什么资源?如果您GET /post会怎样?请以REST的方式考虑,而不是以RPC的方式考虑.

Besides, /post is not a very RESTful URL. What Resource is identified by this? What happens if you GET /post? Please think in REST terms, not in RPC.

这篇关于使用Jersey在Rest Api中传递两个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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