About jersey the entity class as a parameter, and receive back is the code description of the custom object

Source: Internet
Author: User

Jersey receive custom object parameters, and return custom object code examples
The base code is not said, the server code is as follows:

    @PUT
    @Path ("Client")
    @Produces (Mediatype.application_json) public book
    NewBook1 {
        return book;
    }

The client invocation code is as follows:

Public book put () {
        final webtarget webtarget = Client.target (Jaxrs2client.base_uri);
        Final Webtarget pathtarget = Webtarget.path ("Bookresourceget"). Path ("Client");
        Book book = new book (100L, "TEST book", "Hello 2");
        Final book result = Pathtarget.request (). Put (entity.entity (book, mediatype.application_xml), book.class);
        Jaxrs2Client.LOGGER.debug (Pathtarget.geturi ());
        Jaxrs2Client.LOGGER.debug (result);
        return result;
    }

The code analysis is as follows:

   Sent is the book this object in the past, and then receive back book object back book Book
    = The new book (100L, "TEST book", "Hello 2");
    Final book result = Pathtarget.request (). Put (entity.entity (book, mediatype.application_xml), Book.class);

The object code for sending the compound and receiving compound is as follows:
Service Code:

     @GET
     @Path ("Listbook")
     @Consumes ({mediatype.application_xml, mediatype.application_json})
     @Produces ({mediatype.application_xml, mediatype.application_json})
     Public Listbook  Listbook () {book
        book=new book ();
        Book.setbookid (Long.parselong ("the"));
        Book.setbookname ("Yu");
        Book.setpublisher ("QQ");
        Book Book2=new book ();
        Book2.setbookid (Long.parselong ("the"));
        Book2.setbookname ("Yu2");
        Book2.setpublisher ("qq2");
        Arraylist<book> list=new arraylist<book> ();
        List.add (book);
        List.add (BOOK2);
        Listbook booklist=new Listbook ();
        Booklist.setlistbook (list);
        return booklist;

    }

The client invocation code is as follows:

Public Listbook getlist () {
        final webtarget webtarget = Client.target (Jaxrs2client.base_uri);
        Final Webtarget pathtarget = Webtarget.path ("Bookresourceget"). Path ("Listbook");
        Final Listbook result =pathtarget.request (). get (Listbook.class);
        Jaxrs2Client.LOGGER.debug (Pathtarget.geturi ());
        Jaxrs2Client.LOGGER.debug (result);
        return result;
    }

Attached Listbook object code:

Package com.example.client.entity;

Import java.util.List;

Import Com.example.domain.Book;

public class Listbook {
  list<book> listbook;

Public list<book> Getlistbook () {
    return listbook;
}

public void Setlistbook (list<book> listbook) {
    this.listbook = Listbook;
}
}

If you have any questions, leave a comment below.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.