【问题标题】:Sending POST request with POSTMAN to Java REST API and getting null values in the xml response使用 POSTMAN 向 Java REST API 发送 POST 请求并在 xml 响应中获取空值
【发布时间】:2022-04-28 08:34:15
【问题描述】:

我正在用 Java 创建一个 REST 服务,现在我正在构建 post 方法,该方法有 2 个参数,必须在 postman 中输入为 xml(用于测试),并在 java 中以 xml 形式获得响应并将其插入数据库中。

对于初学者,我试图在 POSTMAN 中将值添加为带有键和值的查询参数。响应为 200,但 xml 为 CUI=null Mesaj=null

即使我在 Postman 中为两个键都添加了值,这两个值都为空。

它如何看到这些值?这是java代码:

@Stateless
@Path("/cererepost")
public class HelloWorldResource {
    Resp x = new Resp();
    @EJB
    private NameStorageBean nameStorage;
    /**
     * Retrieves representation of an instance of    helloworld.HelloWorldResource
     * @return an instance of java.lang.String
     */
    @POST
    @Produces("application/xml")
    @Consumes(MediaType.APPLICATION_XML)
    public Response postMsg(@PathParam("cui") String cui,    @PathParam("mesaj") String mesaj)  {
      
        String xmlString = "CUI=" + cui + " Mesaj=" + mesaj;
        Response response = Response.status(200).type(MediaType.TEXT_XML).entity(xmlString).build();
        return response;
    }
}

我应该修改什么,以便在邮递员生成的 xml 中查看我在帖子中发送的参数值?

【问题讨论】:

    标签: java xml rest api postman


    【解决方案1】:

    @PathParam("cui") String cui 此行显示来自客户端的值应作为路径参数而不是查询字符串传递,如下所示:

    正确: /cererepost/some_value

    如果您想在服务器端获取它们作为查询字符串参数,请将@PathParam("cui") 更改为@QueryParam("cui")

    要了解query stringpath variables 之间的区别,请查看this post

    【讨论】:

      【解决方案2】:

      在邮递员中,您可能会错过键入变量名称。它可能与您使用 java 创建的表无关

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 1970-01-01
      • 2016-05-01
      • 2019-02-23
      • 2019-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-13
      • 1970-01-01
      相关资源
      最近更新 更多