簡體   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