gpt4 book ai didi

java - 出现错误 "type=Method Not Allowed, status=405"

转载 作者:行者123 更新时间:2023-12-02 04:19:57 25 4
gpt4 key购买 nike

我可以在使用 POSTMAN 时获得响应,但不能通过代码获得响应。

这是我通过 POSTMAN 所做的事情

RequestMethod.POST
In HEADERS
Content-Type : application/x-www-form-urlencoded
Accept : */*

In BODY
c_id : myname
c_secret : mypassword
g_type : myvalue
c_scope : value2

这就是我发送请求并得到以下响应的方式

{
"access_token": "token_value",
"token_type": "bearer",
"expires_in": 6000,
"scope": "scope_vale",
"jti": "jti_value"
}

在 POSTMAN 中我还禁用了 SSL 验证。

这就是我在 SPRING BOOT 应用程序中编码的方式

@RestController
@RequestMapping(value="/rest/site")
public class CSController {



final String url="url name";


@RequestMapping(path = "/token", method = RequestMethod.POST)
public ResponseEntity<?> getToken() throws JsonProcessingException
{

RestTemplate rt = new RestTemplate();


HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList( MediaType.ALL) );
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);


Map<String, String> bodyParamMap = new HashMap<String, String>();

bodyParamMap.put("c_id", "myname");
bodyParamMap.put("c_secret", "mypassword");
bodyParamMap.put("g_type", "myvalue");
bodyParamMap.put("c_scope", "value2");

String reqBodyData = new ObjectMapper().writeValueAsString(bodyParamMap);
HttpEntity<String> requestEntity = new HttpEntity<>(reqBodyData, headers);

System.out.println( " get token from url 2");

ResponseEntity<String> result = rt.exchange(url,HttpMethod.POST, requestEntity, String.class);



return ResponseEntity.ok(result);
}


}

但是我收到以下错误

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue Jun 18 10:49:58 IST 2019
There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'GET' not supported

我该如何解决这个问题?

最佳答案

您的 Controller 方法支持POST请求而不是GET,因此向浏览器发送请求时将导致method Not allowed

 @RequestMapping(path = "/token", method = RequestMethod.POST) // POST
public ResponseEntity<?> getToken() throws JsonProcessingException
{

RestTemplate rt = new RestTemplate();

How to resolve

从您的方法中删除显式 POST,它将同时适用于 GET 和 POST 请求

@RequestMapping(path = "/token") // Remove explicit mapping here
public ResponseEntity<?> getToken() throws JsonProcessingException
{

关于java - 出现错误 "type=Method Not Allowed, status=405",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56642190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com