删除“类型"从 JSON 输出球衣 moxy [英] Remove "type" from JSON output jersey moxy

查看:23
本文介绍了删除“类型"从 JSON 输出球衣 moxy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从我拥有的 JSON 输出中删除 type.我有一个包含 REST 服务输出的类/bean.我正在使用 jersey-media-moxy 进行转换.

How to remove the type from the JSON output that I have. I have a class/bean that contains output of a REST service.I'm using jersey-media-moxy to do the conversion.

服务

@Resource
public interface MyBeanResource
{
    @GET
    @Path("/example")
    @Produces( MediaType.APPLICATION_JSON )
    public Bean getBean();
}

豆子

@XmlRootElement
class Bean
{
   String a;
}  

我想添加一些功能(用于使用构造函数初始化 bean)

I want to add some functionality (for initializing the bean using the constructor)

class BeanImpl extends Bean
{
    BeanImpl(OtherClass c)
    {
        a = c.toString()
    }
}

输出的 JSON 是:

The outputted JSON is:

{type:"beanImpl", a:"somevalue"}

我不想在我的 JSON 中使用 type.我该如何配置?

I do not want the type in my JSON. How can I configure this?

推荐答案

我在扩展一个类并生成 JSON 时遇到了同样的错误——但只针对顶级(根)类.作为一种解决方法,我使用 @XmlType(name="") 注释我的 子类,这会阻止生成的 type 属性出现在我的 JSON 中.

I get the same error when I extend a class and generate JSON -- but only for a top-level (root) class. As a workaround, I annotate my subclass with @XmlType(name=""), which prevents the generated type property from appearing in my JSON.

Blaise,我不确定为什么会这样.有什么想法吗?

Blaise, I'm not sure why this works. Any thoughts?

这篇关于删除“类型"从 JSON 输出球衣 moxy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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