gpt4 book ai didi

java - 如何将java对象从客户端传递到Web服务

转载 作者:行者123 更新时间:2023-12-02 02:59:08 25 4
gpt4 key购买 nike

您好,我对 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();
}

该接口(interface)由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
}

关于java - 如何将java对象从客户端传递到Web服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57061565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com