[英]How to pass java Object from client to Webservice
您好,我是Web服务的新手,我正在尝试将Java对象传递给我创建的Web服务,该服务具有一个采用Java对象的Web方法。 我在下面附上我的代码。
Web服务类:
@WebService
public class WsTicketService {
WsTicketStore ticketstore = new WsTicketStore();
@WebMethod
public void createTicket(Ticket ticket) {
System.out.println("Requested to store a new ticket");
Ticket myTicket = ticketstore.storeNewTicket(ticket.getReporter(), ticket.getTopic(),ticket.getDescription(), ticket.getType(), ticket.getPriority());
System.out.println("Ticket Stored");
}
}
创建票证的客户端类我想将此票证发送到上述Web服务,但在service.createTicket(Ticket)行上收到错误,该类型不适用于参数。
public class WsTicketManagementBackend implements TicketManagementBackend {
HashMap<Integer, Ticket> localTicketStore = new HashMap<>();
AtomicInteger nextId;
WsTicketServiceService client;
WsTicketService service;
public WsTicketManagementBackend() {
nextId = new AtomicInteger(1);
this.client = new WsTicketServiceService();
service = client.getWsTicketServicePort();
}
@Override
public void triggerShutdown() {
}
@Override
public Ticket createNewTicket(String reporter, String topic, String description, Type type, Priority priority) {
Ticket ticket = new Ticket(nextId.getAndIncrement(), reporter, topic, description, type, priority);
localTicketStore.put(ticket.getId(), ticket);
service.createTicket(ticket);
return (Ticket) ticket.clone();
}
此接口由wsimport工具生成:
public interface WsTicketService {
/**
*
* @param arg0
*/
@WebMethod
@RequestWrapper(localName = "createTicket", targetNamespace = "http://implementation.remote.ws.backend.rz.uniba.de/", className = "de.uniba.rz.backend.ws.remote.implementation.CreateTicket")
@ResponseWrapper(localName = "createTicketResponse", targetNamespace = "http://implementation.remote.ws.backend.rz.uniba.de/", className = "de.uniba.rz.backend.ws.remote.implementation.CreateTicketResponse")
@Action(input = "http://implementation.remote.ws.backend.rz.uniba.de/WsTicketService/createTicketRequest", output = "http://implementation.remote.ws.backend.rz.uniba.de/WsTicketService/createTicketResponse")
public void createTicket(
@WebParam(name = "arg0", targetNamespace = "")
Ticket arg0);
}
您可以尝试发送带有Ticket对象属性的json字符串,然后创建它,如下所示:
createTicket(String jsonTicket){
// Instantiate a jsonObject (you can use any json library you want, I recommend PrimeFaces JSONObject)
JSONObject json = new JSONObject(jsonTicket);
// So here you make a new ticket
Ticket ticket = new Ticket();
// Set the properties of the ticket to each attribute of your json
ticket.setName(json.getString("name"));
// and so on
}
我使用JAXB通过在客户端将Java对象编组为xml文档,然后在Web服务器端将其编组为java对象来解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.