Java 获取错误“;类型=不允许的方法,状态=405“;
当我使用邮递员时,我能够得到回复,但不是通过代码 这就是我通过邮递员所做的Java 获取错误“;类型=不允许的方法,状态=405“;,java,spring,spring-boot,Java,Spring,Spring Boot,当我使用邮递员时,我能够得到回复,但不是通过代码 这就是我通过邮递员所做的 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_
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"
}
在《邮递员》中,我还禁用了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);
}
}
如何解决此问题?您的控制器方法支持
POST
请求而不是GET
,因此在向浏览器发送请求时,将导致方法不允许
@RequestMapping(path = "/token", method = RequestMethod.POST) // POST
public ResponseEntity<?> getToken() throws JsonProcessingException
{
RestTemplate rt = new RestTemplate();
@RequestMapping(path=“/token”,method=RequestMethod.POST)//POST
public ResponseEntity getToken()引发JsonProcessingException
{
RestTemplate rt=新的RestTemplate();
如何解决
从方法中删除显式POST,它将同时适用于GET和POST请求
@RequestMapping(path = "/token") // Remove explicit mapping here
public ResponseEntity<?> getToken() throws JsonProcessingException
{
@RequestMapping(path=“/token”)//在此处删除显式映射
public ResponseEntity getToken()引发JsonProcessingException
{
非常感谢,我知道问题出在哪里了。我按照你说的做了同样的事情,但是我得到了一个新的证书错误请求处理失败;嵌套异常是org.springframework.web.client.ResourceAccessException:POST请求时的i/O错误“https://url-for-token":
`sun.security.validator.validator异常:PKIX路径生成失败:sun.security.provider.certpath.SunCertPathBuilderException:找不到请求目标的有效证书路径;嵌套异常为javax.net.ssl.SSLHandshakeException:sun.security.validator.validator异常:PKIX路径生成失败:sun.security.provider.certpath.SunCertPathBuilderException:在POSTMAN中找不到请求目标的有效证书路径`我还禁用了SSL验证。您使用的url不起作用,请检查服务器上是否存在防火墙问题,或者查看您传递的url是否正确,因为我可以在POSTMAN中获得响应。因此,我接下来应该检查什么?
@RequestMapping(path = "/token") // Remove explicit mapping here
public ResponseEntity<?> getToken() throws JsonProcessingException
{