【问题标题】:How to get only one String parameter from request in Java?如何从 Java 中的请求中仅获取一个字符串参数?
【发布时间】:2020-05-06 15:08:45
【问题描述】:

我已经创建了 POST 方法:

    @POST
    @Path("/test")
    @JsonView({ TestView.class })
    public Response test(@Valid String testParameter) {
        return testController.doTest(testParameter);
    }

在请求中我发送一个参数:

{
  "testParameter": "Hello"
}

我想映射到方法中的“testParameter”参数。类消耗/产生MediaType.APPLICATION_JSON。有任何可用的注释可以让我获取此参数吗? 还是我必须重新映射整个 JSON(String) 并获取此值?

【问题讨论】:

  • 描述了多个选项,例如here

标签: java json post jackson jax-rs


【解决方案1】:

您只需要一个请求对象类,其中包含一个名为testParameter 的字符串字段。 将String 参数替换为此请求对象并从中检索您的值。

【讨论】:

  • 我想避免为此创建特殊对象
【解决方案2】:

如果您不想创建 Class,可以使用 Map<String, String> 作为 requestbody。

public Response test(Map<String, String> map) {
        return testController.doTest(map.get("testParameter"));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-16
    • 1970-01-01
    • 2020-03-01
    • 1970-01-01
    • 2010-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多