作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
您好,我对 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/
我是一名优秀的程序员,十分优秀!