我有一个CXF Web服务,其中只有一个参数应该在有效负载上发送。所有其他参数都在URL上发送:
参数类:
class Book
{
String name;
String author;
//getters, setters
}
服务:
@Post
@Path("/addBook/{roomId}/{bookId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@WebResult(name = "addBookResponse")
public LibraryResponse addBook(
@PathParam("roomId") @WebParam(name = "roomId") String roomId,
@PathParam("bookId") @WebParam(name = "bookId") String bookId,
@WebParam(name = "book") Book book);
我在REST客户端发送的有效负载:
{
"name":"War and Peace",
"author":"Lev Tolstoy"
}
在这个简单的例子中它起作用。 :)
现在,我有另一个CXF Web服务方法,其中有两个参数应该在有效负载上发送:
@PUT
@Path("/replaceBook/{roomId}/{bookId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@WebResult(name = "replaceBookResponse")
public LibraryResponse replaceBook(
@PathParam("roomId") @WebParam(name = "roomId") String roomId,
@PathParam("bookId") @WebParam(name = "bookId") String bookId,
@WebParam(name = "originalBook") Book originalBook,
@WebParam(name = "newBook") Book newBook);
我应该发送什么作为有效载荷?我试图发送以下有效载荷,但它没有用。
[
{
"name": "The Da Vinci Code",
"author": "Dan Brown"
},
{
"name": "War and Peace",
"author": "Lev Tolstoy"
}
]
可能以下是如何做到这一点:
https://stackoverflow.com/a/13253070/784980
https://cwiki.apache.org/confluence/display/CXF20DOC/JAX-RS+Multiparts
https://ext2xhb.wordpress.com/2011/06/08/using-cxf-attachment-safely/
答案 0 :(得分:0)
试试这个:
{
"originalBook": {
"name": "The Da Vinci Code",
"author": "Dan Brown"
},
"newBook": {
"name": "War and Peace",
"author": "Lev Tolstoy"
}
}