Rest API无法返回包含其他对象的Object的ArrayList

时间:2018-07-03 16:21:33

标签: java spring rest jax-rs

我的POJO类是这样的

1。

/*crating MesageModel like this **/
/** ==========================================================================**/

     package tech.sach.webapp.webserve.subpack;

    import javax.xml.bind.annotation.XmlRootElement;

        import tech.sach.webapp.webserve.link.Link;

        @XmlRootElement
        public class MessageModel{

            int mesageid;
            String mesgForm;
            String name;


            Link links;    //id i comment this it works fine

            public MessageModel() {
            }

            public MessageModel(int id,String a,String b,Link link )
            {
                this.mesageid=id;
                this.mesgForm=a;
                this.name=b;
                this.links=link;
            }

            public MessageModel(int id,String a,String b )
            {
                this.mesageid=id;
                this.mesgForm=a;
                this.name=b;
            }


            public int getMesageid() {
                return mesageid;
            }

            public void setMesageid(int mesageid) {
                this.mesageid = mesageid;
            }

            public String getMesgForm() {


                return mesgForm;
            }

            public void setMesgForm(String mesgForm) {
                this.mesgForm = mesgForm;
            }

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            public Link getLinks() {
                return links;
            }

            public void setLinks(Link links) {
                this.links = links;
            }   
        }

2。我的第二个POJO课

  /*Link is the object used in MessageModel  */ 

      package tech.sach.webapp.webserve.link;

            public class Link {

                String url;
                String rel;

                public Link(String url,String rel) {
                    this.url=url;
                    this.rel=rel;
                }
                public String getUrl() {
                    return url;
                }
                public void setUrl(String url) {
                    this.url = url;
                }
                public String getRel() {
                    return rel;
                }
                public void setRel(String rel) {
                    this.rel = rel;
                }       

            }

我正尝试使用Rest JAX-RS API这样发送响应。

以下是我尝试调用的实际休息资源。

返回数组列表

   package tech.sach.webapp.webserve.subpack;

        import java.net.URI;
        import java.net.URL;
        import java.util.ArrayList;
        import java.util.List;

        import javax.inject.Scope;
        import javax.ws.rs.ApplicationPath;
        import javax.ws.rs.Consumes;
        import javax.ws.rs.GET;
        import javax.ws.rs.POST;
        import javax.ws.rs.Path;
        import javax.ws.rs.Produces;
        import javax.ws.rs.core.Context;
        import javax.ws.rs.core.MediaType;
        import javax.ws.rs.core.UriInfo;
        import javax.xml.bind.annotation.XmlRootElement;

        import tech.sach.webapp.webserve.link.Link;


        @Path("subpack")
        @Consumes(MediaType.APPLICATION_JSON)
        @Produces(MediaType.APPLICATION_JSON)
        public class SubpackageClassCall {
        /*Creating arraylist*/  
            ArrayList<MessageModel> aArr=new ArrayList<MessageModel>();

           /*  Getting response*/
            @GET
            public ArrayList<MessageModel> getSubpack()
            {

                //Creating arraylikst here with dynamic MessageModel object

                aArr.add(new MessageModel(1, "sac", "sdf"));
                aArr.add(new MessageModel(2, "adsdsac", "csxcxcdf"));
                aArr.add(new MessageModel(3, "sadasdcxcxcsac", "swwwwwdf"));
                return aArr;
            }

            }

我试图在邮递员或浏览器中呼叫“ http://localhost:8080/webserve/webapi/subpack ”,但遇到错误

   **"SEVERE: MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList, genericType=java.util.ArrayList<tech.sach.webapp.webserve.subpack.MessageModel>."**

因此,在'SubpackageClassCall'类中,我正在动态创建'MessageModel'对象,但未执行与其组成的对象'Link'相关的任何事情。

如果我从“ MessageModel”中删除了组成的“ Link”对象,则其工作正常。

但是当我在'MessageModel'中包含'Link'对象时,为什么它不起作用?

0 个答案:

没有答案