简体   繁体   English

Java Spring REST调用参数

[英]Java Spring REST call parameters

Let's assume there's a following Java-file, Foo.java:假设有以下 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;
     }
}

Then there's another file, RESTcontroller.java:然后是另一个文件,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)
        ...
    }
}

Then in third file, http.js the 'getFoo' endpoint is called:然后在第三个文件 http.js 中调用“getFoo”端点:

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

So, the question is, how the query parameters are used to construct the Foo parameter, when the second parameter needed in the Foo-constructor is missing?那么问题来了,当 Foo 构造函数中需要的第二个参数缺失时,如何使用查询参数来构造 Foo 参数? Which of the two constructors is used and at which point?使用了两个构造函数中的哪一个,在什么时候使用? Does this have something to do with Spring-framework?这与 Spring 框架有关吗? The example codes here are a version of code that has been proven to function.这里的示例代码是经过 function 验证的代码版本。

It all have to do with Spring (or more precisely the Spring hidden configuration magic).这一切都与Spring (或更准确地说是Spring隐藏配置魔法)有关。

The org.springframework.web.method.support.HandlerMethodArgumentResolver resolved run-time implementation is the component responsible of transforming request parameters into an object, a process called parameter mapping. org.springframework.web.method.support.HandlerMethodArgumentResolver解析运行时实现是负责将请求参数转换为 object 的组件,该过程称为参数映射。

In the described example, what the resolver will use is the no-arg constructor along with the setters holding the received parameters names, ie, setFirst and setThird在所描述的示例中,解析器将使用无参数构造函数以及保存接收到的参数名称的设置器,即setFirstsetThird

The 3 arg constructor is never called, and all your POJO needs to implement is a standard no-argument constructor and setters and getters for the instance variables.永远不会调用 3 arg 构造函数,您的POJO需要实现的只是一个标准的无参数构造函数以及实例变量的 setter 和 getter。

You got the rest thing wrong - using GET rest method is not used to construct an object, but to simply retrieve it from the service/db.您弄错了 rest 错误 - 使用 GET rest 方法不用于构造 object,而只是从服务/数据库中检索它。

In this case, the query params that can be used are such that will decide which objects to return (by its id or other parameters).在这种情况下,可以使用的查询参数将决定返回哪些对象(通过其 id 或其他参数)。 You do not need to pass the foo object to the method, as GET usually don't have a request body您不需要将 foo object 传递给该方法,因为 GET 通常没有请求正文

So the get method will look something like:所以 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);
}

for constructing new objects, you should use POST method, and it is better to pass the new object as a json in the request's body.对于构造新对象,您应该使用 POST 方法,并且最好将新的 object 作为 json 在请求的正文中传递。 it will be passed to the controller method as an parameter:它将作为参数传递给 controller 方法:

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

And one last thing, the convention when using REST is to use the same path, ie /foo, and the rest method will determine if it's create (POST), update (PUT) or get (GET).最后一件事,使用 REST 时的约定是使用相同的路径,即 /foo,并且 rest 方法将确定它是创建 (POST)、更新 (PUT) 还是获取 (GET)。 You don't need to call your path /getFoo你不需要调用你的路径 /getFoo

The constructor being used is the default one (no args).使用的构造函数是默认构造函数(无参数)。 And yes, Spring is setting the values of your fields.是的,Spring 正在设置您的字段值。

If the second parameter is missing, the value of second parameter is null .如果缺少第二个参数,则第二个参数的值为null The 3 arg constructor is also called, but the second parameter is set null when call the constructor.@tmarwen也调用了 3 arg 构造函数,但是调用构造函数时设置了第二个参数 null。@tmarwen

I don't understand exactly what you mean, but I understand that RequestBody is the default.我不明白你的意思,但我知道 RequestBody 是默认值。

@RequestBody Foo foo same thing. @RequestBody Foo foo同样的事情。 For Example,例如,

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

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

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