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.