简体   繁体   English

带有@ Xml *批注的Java EE RESTful服务JSON编组415状态

[英]Java EE RESTful service JSON marshalling 415 status with @Xml* annotations

I have simple Java EE RESTful service, which used JAX-RS (Jersey), JPA (EclipseLink) and JAXB (EclipseLink MOXy) on Payara 4.1.1.154 (GlassFish 4.1.1 fork - http://payara.fish ). 我简单的Java EE RESTful服务,它使用JAX-RS(新泽西州),JPA(的EclipseLink)和JAXB(EclipseLink的莫西)上似鲭水狼牙鱼4.1.1.154(GlassFish的4.1.1叉- http://payara.fish )。

I use two entityes Radio and Stream with bidirectional link between them: 我使用两个实体Radio和Stream,它们之间具有双向链接:

Radio entity: 无线电实体:

@Entity
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Radio {

    @Id
    @GeneratedValue
    private long id;

    @Column(unique = true, nullable = false)
    private String name;

    @Column(nullable = false)
    private String genre;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @XmlIDREF
    private List<Stream> streams;

    @Version
    private long version;
    ... constructors, getters, setters
}

Streams entity 流实体

@Entity
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Stream {

    @Id
    @GeneratedValue
    @XmlID
    private long id;

    private String name;
    private String mountPoint;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn
    private Radio radio;

    @Version
    private long version;
    ... constructors, getters, setters
}

And RESTFul resource handler: 和RESTFul资源处理程序:

@Path("/radios")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class RadioResource {

    @Inject
    private RadioService radioService;

    public RadioResource() {}

    @GET
    @Path("/{radioId}")
    public Radio getById(@PathParam("radioId") long id) throws com.roks.radio.rt.services.NotFoundException {
        return radioService.findById(id);
    }

    @POST
    public Response create(Radio radio, @Context UriInfo uriInfo) throws AlreadyExistsException {
        radioService.create(radio);
        return Response.created(
                uriInfo.getAbsolutePathBuilder().path(
                        String.valueOf(radio.getId())
                ).build()
        ).entity(radio).build();
    }
}

When doing POST request with type of application/json server responce with 415 status code (mediatype not supported). 使用应用程序/ json服务器类型执行POST请求时,将以415状态代码响应(不支持媒体类型)。 When I remove @XmlID and @XmlIDREF annotations from code, all working fine (201 Created responce), except missing "streams" field in JSON output. 当我从代码中删除@XmlID和@XmlIDREF批注时,所有工作正常(201创建响应),但在JSON输出中缺少“ streams”字段。

I think "streams" not present because of circular reference in result JSON object when streams list empty. 我认为由于流列表为空时结果JSON对象中的循环引用,因此不存在“流”。 But I try to remove this reference with @XmlID and @XmlREFID annotations, but JSON marshalling stop working. 但是我尝试使用@XmlID和@XmlREFID批注删除此引用,但是JSON编组停止工作。

In JSON output i mainly need "streams" field with streams IDs. 在JSON输出中,我主要需要带有流ID的“流”字段。

What is wrong with my code? 我的代码有什么问题?

try to use the annotation 尝试使用注释

@JsonProperty

on the getters you want to be marshalled in json (org.codehaus.jackson.annotate.JsonProperty). 在您想要在json(org.codehaus.jackson.annotate.JsonProperty)中被整理的getter上。

Are you using jersey-media-moxy.jar in your project? 您在项目中使用的是jersey-media-moxy.jar吗? It's purpose is to bind objects to JSON. 目的是将对象绑定到JSON。 I used it once but i'm not sure i still have the source. 我曾经使用过一次,但我不确定我仍然有来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM