【问题标题】:Json object sent by postman received with null members邮递员发送的 Json 对象收到空成员
【发布时间】:2016-12-28 00:14:26
【问题描述】:

这是这个问题的后续问题:

sending nested json object using postman

我正在尝试使用 Postman 将 Json 对象发送到 Jersey 网络服务。

我是这样做的:

在泽西一侧:

@Path("/testMethod")
@POST
@UnitOfWork
public short testMethod(@NotNull @BeanParam Test test)
{ ... }

测试类是一个简单的类:

public class Test
{
    public String field;

    public Test()
    {

    }
}

在邮递员方面,我正在发送一条 POST 消息,其中 Body 设置为 raw,内容类型设置为 Json(application/json)。身体本身是:

{
 "field" : "12"
}

发送这个请求时,接收到的参数中的field为空……这是为什么呢?

【问题讨论】:

    标签: java json postman


    【解决方案1】:

    如果您只想将 json 数据映射到对象,则只需删除 @BeanParam 即可正常工作。

    但是,如果您想使用@BeanParam 功能,您应该从第一个参数中删除该注释,并添加带有@BeanParam 注释的第二个参数:

    @Path("/testMethod")
    @POST
    @UnitOfWork
    public short testMethod(@NotNull Test test, @BeanParam ExtraTest extraTest)
    { ... }
    

    然后使用额外的字段映射实现您的 ExtraTest 类:

    public class ExtraTest
    {
        @HeaderParam("Accept")
        public String acceptHeader;
    
        /* 
        your other header params, path params and so on...
        ...
        */
    } 
    

    @BeanParam 是允许您轻松访问附加请求信息(标头字段、查询参数等)的功能。它不能与您的 json 数据对象映射混合。

    更多信息:

    https://abhirockzz.wordpress.com/2014/07/21/new-in-jax-rs-2-0-beanparam-annotation/

    https://jersey.java.net/apidocs/2.22/jersey/javax/ws/rs/BeanParam.html

    【讨论】:

    • 什么时候应该接受没有 BeanParam 注释的 Json 对象,什么时候应该使用它?
    • 检查我编辑的答案。 @BeanParam 是一种机制,可让您轻松访问其他请求信息(标头、查询参数等)。
    猜你喜欢
    • 2014-12-29
    • 2017-08-17
    • 2021-05-19
    • 2022-01-07
    • 2020-04-07
    • 2020-05-26
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    相关资源
    最近更新 更多