繁体   English   中英

Java Spring REST调用参数

[英]Java Spring REST call parameters

假设有以下 Java 文件 Foo.java:

public class Foo {
     private String first;
     private String second;
     private String third;

     public Foo(){
     }
     public Foo(String first, String second, String third){
          this.first = first;
          this.second = second;
          this.third = third;
     }
     public String getFirst(){
          return first;
     }
     public String getSecond(){
          return second;
     }
     public String getThird(){
          return third;
     }
     public void setFirst(String first){
          this.first = first;
     }
     public void setSecond(String second){
          this.second = second;
     }
     public void setThird(String third){
          this.third = third;
     }
}

然后是另一个文件,RESTcontroller.java:

import Bar;
import Foo;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/rest")
public class RESTController {
    @RequestMapping(path = "/getFoo", method = RequestMethod.GET)
    public void getFoo(Foo foo, HttpServletResponse response) {
        Bar bar = Bar.getBar(foo)
        ...
    }
}

然后在第三个文件 http.js 中调用“getFoo”端点:

class Http {
    getFoo(url, first, third) {
        return `${url}getFoo?first=${first}&third=${third}`;
    }
}

那么问题来了,当 Foo 构造函数中需要的第二个参数缺失时,如何使用查询参数来构造 Foo 参数? 使用了两个构造函数中的哪一个,在什么时候使用? 这与 Spring 框架有关吗? 这里的示例代码是经过 function 验证的代码版本。

这一切都与Spring (或更准确地说是Spring隐藏配置魔法)有关。

org.springframework.web.method.support.HandlerMethodArgumentResolver解析运行时实现是负责将请求参数转换为 object 的组件,该过程称为参数映射。

在所描述的示例中,解析器将使用无参数构造函数以及保存接收到的参数名称的设置器,即setFirstsetThird

永远不会调用 3 arg 构造函数,您的POJO需要实现的只是一个标准的无参数构造函数以及实例变量的 setter 和 getter。

您弄错了 rest 错误 - 使用 GET rest 方法不用于构造 object,而只是从服务/数据库中检索它。

在这种情况下,可以使用的查询参数将决定返回哪些对象(通过其 id 或其他参数)。 您不需要将 foo object 传递给该方法,因为 GET 通常没有请求正文

所以 get 方法看起来像:

// all items
@RequestMapping(path = "/foo", method = RequestMethod.GET)
public List<Foo> getFoos() {
    return fooService.getAll();
}

// single item
@RequestMapping(path = "/foo/{id}", method = RequestMethod.GET)
public Foo getFoos(@PathParam String id) {
    return fooService.getByid(id);
}

对于构造新对象,您应该使用 POST 方法,并且最好将新的 object 作为 json 在请求的正文中传递。 它将作为参数传递给 controller 方法:

@RequestMapping(path = "/foo", method = RequestMethod.POST)
public Foo createFoo(@RequestBody Foo foo) {
    return fooService.save(foo);
}

最后一件事,使用 REST 时的约定是使用相同的路径,即 /foo,并且 rest 方法将确定它是创建 (POST)、更新 (PUT) 还是获取 (GET)。 你不需要调用你的路径 /getFoo

使用的构造函数是默认构造函数(无参数)。 是的,Spring 正在设置您的字段值。

如果缺少第二个参数,则第二个参数的值为null 也调用了 3 arg 构造函数,但是调用构造函数时设置了第二个参数 null。@tmarwen

我不明白你的意思,但我知道 RequestBody 是默认值。

@RequestBody Foo foo同样的事情。 例如,

  • @PathVariable => `${SERVER_URL}getFoo?10
  • @RequestParam => `${SERVER_URL}getFoo?id=10

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM