[英]No response for PATCH method in Spring Boot
I'm trying to create a REST API.我正在尝试创建一个 REST API。 GET & POST method are working as expected. GET & POST 方法按预期工作。 However for PATCH method I'm getting unexpected behaviour.但是对于 PATCH 方法,我得到了意想不到的行为。 Below in my controller code.下面在我的控制器代码中。
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController()
@RequestMapping("v1/test")
public class TestController {
@GetMapping(value = "get-test")
public String getTest(){
return "Get Success";
}
@PatchMapping(value = "patch-test")
public String patchTest(){
return "Patch Success";
}
@PostMapping(value = "post-test")
public String postTest(){
return "Post Success";
}
}
When I hit PATCH method loader keeps spinning & not getting any response.当我点击 PATCH 方法时,加载程序一直在旋转并且没有得到任何响应。 If I hit the PATCH after GET or POST, I get the previous API response for PATCH method.如果我在 GET 或 POST 之后点击 PATCH,我会得到 PATCH 方法的先前 API 响应。 This same code is working on other machines.相同的代码正在其他机器上运行。 I've tried with both IntelliJ & Eclipse, behaviour is same.我已经尝试过 IntelliJ 和 Eclipse,行为是一样的。 Tried this from multiple clients like postman & curl.从邮递员和卷曲等多个客户那里尝试过这个。 Attaching the screenshot.附上截图。 This was working previously.这是以前的工作。 haven't done any changes & now it's not working没有做任何改变 & 现在它不工作
You have two mistakes.你有两个错误。
1- you should write "value" in the request mapping parameter as follows: 1- 您应该在请求映射参数中写入“值”,如下所示:
@RequestMapping(value = "v1/test")
2- you should write path instead of value in patch parameter and put / character before url as follows: 2-您应该在补丁参数中写入路径而不是值,并将 / 字符放在 url 之前,如下所示:
@PatchMapping(path = "/patch-test")
Just try in simple way, this works for me within a second只需以简单的方式尝试,这会在一秒钟内为我工作
Try with below code尝试使用以下代码
@PatchMapping("/patchtest")
public String patchtest(){
return "Patch Success";
}
Also dont forget to mention @RestController annotation另外不要忘记提及@RestController 注释
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.