删除“类型"从 JSON 输出球衣 moxy

2022-01-21 00:00:00 json marshalling java jersey 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?

相关文章