Java Spring REST调用参数

Java Spring REST调用参数,java,spring,Java,Spring,让我们假设有以下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;

让我们假设有以下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框架有关吗?这里的示例代码是一个已被证明有效的代码版本。

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

解析的运行时实现是负责将请求参数转换为对象的组件,该过程称为参数映射

在所描述的示例中,解析器将使用no arg构造函数以及保存所接收参数名称的设置器,即setFirst和setThird


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

rest错误-使用GET-rest方法不是用来构造对象,而是简单地从服务/db检索对象

在这种情况下,可以使用的查询参数将决定返回哪些对象(通过其id或其他参数)。您不需要将foo对象传递给该方法,因为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);
}

最后一点,使用REST时的约定是使用相同的路径,即/foo,REST方法将确定它是创建(POST)、更新(PUT)还是获取(get)。您不需要调用path/getFoo

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

如果缺少第二个参数,则第二个参数的值为
null

也会调用3参数构造函数,但调用构造函数时第二个参数设置为null。@tmarwen

我不太明白您的确切意思,但我知道RequestBody是默认值

@RequestBody Foo Foo
同样的事情。 比如说,

  • @PathVariable=>`${SERVER\u URL}getFoo?10
  • @RequestParam=>`${SERVER\u URL}getFoo?id=10

您是否尝试在构造函数中设置断点并查看哪个断点被命中?这也将回答您所有的其他问题。因为您可以看到调用它时使用了什么来回答“如何使用查询参数”。堆栈跟踪会告诉你是否是spring做的,spring框架并不关心这种情况下的非默认构造函数。它总是使用默认构造函数来创建对象,然后使用setter方法来设置提供的值。我更感兴趣的是springrest语法背后的理论,以及如何使用查询参数来构造Foo参数,以及在哪里?除了RestController和RequestMapping之外,没有Spring注释。
Foo
参数是基于接收到的查询参数的搜索对象。这是一个完全合理的模式。@user2478398-get方法返回void在rest中是不合理的模式该方法的参数为
HttpServletResponse
。他可以写任何他想写的标题/正文。这不是最好的方法,但仅仅因为方法的
void
并不意味着它不返回数据。
@RequestMapping(path = "/foo", method = RequestMethod.POST)
public Foo createFoo(@RequestBody Foo foo) {
    return fooService.save(foo);
}