java - JPOS Restful API 发送和接收数据
问题描述
我从jpos-rest.pdf在 JPOS 中配置 RESTFul API 。
问题是我无法从客户端接收数据,但我可以向客户端发送数据。
在Echo.java
课堂上通过下面的代码我可以发送数据:
package org.jpos.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.HashMap;
import java.util.Map;
@Path("/echo")
public class Echo {
@GET
@Produces({MediaType.APPLICATION_JSON})
public Response echoGet() {
Map<String, Object> resp = new HashMap<>();
resp.put("success", "true");
resp.put("Name", "Hamid");
resp.put("Family", "Mohammadi");
Response.ResponseBuilder rb = Response.ok(resp, MediaType.APPLICATION_JSON).status(Response.Status.OK);
return rb.build();
}
}
解决方案
感谢@Sabir Khan,我将代码更改为:
@Path("/echo")
public class Echo {
@PUT
@Produces({MediaType.APPLICATION_JSON})
@Consumes(MediaType.TEXT_PLAIN)
@Path("/{name}/{family}")
public Response echoGet(
@PathParam("name") String name,
@PathParam("family") String family,
String Desc
) {
Map<String, Object> resp = new HashMap<>();
resp.put("success", "true");
resp.put("Name", name);
resp.put("Family", family);
resp.put("Desc", Desc);
Response.ResponseBuilder rb = Response.ok(resp,
MediaType.APPLICATION_JSON).status(Response.Status.OK);
return rb.build();
}
}
推荐阅读
- elasticsearch - 为什么 Kibana 拒绝使用 PUT 的幂等更新操作
- python - 从实例而不是类继承 [Python 3]
- batch-file - 批处理用冒号分隔并在一行中返回结果
- ios - iPhone 文档中未创建文件
- python - Pandas to_sql 向 db 表发送额外的空白行
- azure - HTTP Post 在本地工作,但在 Azure 上返回 400
- android - 不能使用 typedef 枚举
- reactjs - 使用不适用于 GET 的字符串将 POST 数据发送到 WebView
- arrays - 在构造函数中创建数组与在 main 中创建数组的区别
- java - Java中状态切换的最佳实现是什么?