【问题标题】:Spring Boot - How can I pass custom values in HTTP Post api?Spring Boot - 如何在 HTTP Post api 中传递自定义值?
【发布时间】:2018-02-18 07:52:21
【问题描述】:

我是 Spring Boot 新手,我很难理解如何传递数据。例如:

我想将这些数据传递给我的服务器:

{
    "code", 1,
    "name": "C01"
}

所以我创建了 always 一个自定义对象,其中包含代码和名称作为属性来拥有这个 http post api?

@RequestMapping(value = "/new/", method = RequestMethod.POST)
public ResponseEntity<?> createOrder(@RequestBody CustomObject customObject){
    ...
}

我看到的另一个解决方案可能是这样,但我不能传递数字(int 代码),对吧?

@RequestMapping(value = "/new/{code}/{name}", method = RequestMethod.POST)
    public ResponseEntity<?> createOrder(@PathVariable("code") int code, @PathVariable("name") String name) {
        ...
}

亲切的问候:)

【问题讨论】:

    标签: spring spring-mvc spring-boot http-post spring-restcontroller


    【解决方案1】:

    您可以将code 和name 传递为PathVariables,就像在您的示例中一样:

    @RequestMapping(value = "/new/{code}/{name}")
    public ResponseEntity<?> createOrder(@PathVariable("code") int code, @PathVariable("name") String name) {
        ...
    }
    

    根据文档,PathVariable 可以是 int 或 String 或 long 或 Date:

    @PathVariable 参数可以是任何简单类型,例如 int、long、Date 等。Spring 会自动转换为适当的类型,如果转换失败则抛出 TypeMismatchException。

    您还可以像这样定义Map&lt;String, Object&gt; 类型的PathVariable:

    @RequestMapping(value = "/new/{code}/{name}")
    public ResponseEntity<?> createOrder(@PathVariable("map") Map<String, Object> map) {
        Integer code = (Integer) map.get("code");
        String name = (String) map.get("name");
        ...
    }
    

    您甚至可以使用@RequestParam 并以 URL 查询参数的形式提供数据。

    因此,可以通过多种方式将数据传递给 Spring MVC 控制器(更多详细信息in the docs),但我认为发布复杂数据的约定(我的意思是“复杂”不止一个状态)是定义一个请求正文,其中包含该复杂状态的序列化形式,即您在问题的第一个示例中显示的内容:

    @RequestMapping(value = "/new/", method = RequestMethod.POST)
    public ResponseEntity<?> createOrder(@RequestBody CustomObject customObject){
        ...
    }
    

    【讨论】:

    • 完美,谢谢...所以如果我想使用 Map 我必须传递一个 JSON 对象?像 { "code", 1, "name": "C01"}?谢谢:)
    • 是的,这是可能的,但只是重申一下:“发布复杂数据的约定(“复杂”我的意思是不止一个状态)是定义一个包含序列化表单的请求正文复杂状态”这在 REST API 中尤其如此,因为这种复杂状态通常由服务器端的实体(即类)表示。因此,我认为您定义自定义对象的原始方法比发布Map 更常见。希望对您有所帮助。
    【解决方案2】:

    如果这个问题是关于 RESTful 最佳实践的,因为您正在开发用于创建 Order 对象的 web 服务,这就是我的设计方式

    Order.java

    public class Order {
    
      private Integer code;
      private String name;
    
      public Integer getCode() {
        return code;
      }
    
      public void setCode(final Integer code) {
        this.code = code;
      }
    
      public String getName() {
        return name;
      }
    
      public void setName(final String name) {
        this.name = name;
      }
    }
    

    控制器

    @RequestMapping(value = "/orders", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseEntity<Order> createOrder(@Valid @RequestBody Order order){
        ...
    }
    

    从技术上讲,你可以做很多事情来实现同样的事情,但这不会是一个 RESTful 服务,它充其量只是一个 RPC。

    【讨论】:

    • 嗨,谢谢,但如果代码用于订单,名称用于客户,我有一个创建这个新对象(带有代码和名称)来传递这些数据(没有使用 @PathVariable)?再次感谢
    • @Hazlo8 你能解释一下你的要求吗
    猜你喜欢
    • 2018-12-02
    • 2019-08-23
    • 2019-12-10
    • 1970-01-01
    • 2017-12-03
    • 1970-01-01
    • 2014-12-01
    • 1970-01-01
    • 2017-10-13
    相关资源
    最近更新 更多