2021-02-26

Getting Bad Request while trying to consume REST API in Java SpringBoot

Hi I am trying to consume a REST endpoint using POST, but I am getting the error below. The endpoint gives proper response in POSTMAN, but I am getting error in Java. Please let me know where the mistake is.

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request: [{
  "error":"unsupported_grant_type",
  "error_description":"The given grant_type is not supported"
}]] with root cause

org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request: [{
  "error":"unsupported_grant_type",
  "error_description":"The given grant_type is not supported"
}]

Below is code:

Controller:

public class MemberController {

private static final Logger log = LoggerFactory.getLogger(MemberController.class);

@Autowired
MemberService memberService;

@PostMapping(value = "/token",  headers = "Accept=application/json")
public String getToken() {
    log.info("Test getToken method");
    return memberService.callTokenService();
}

Service Class:

public class MemberService {    
    private static final Logger log = LoggerFactory.getLogger(MemberService.class);    
    private RestTemplate restTemplate = new RestTemplate();    
    final String tokenURL = "-------";    

    public String callTokenService() {

        log.info("Inside Service");     
        ResponseEntity<TokenProperties>  response = restTemplate.postForEntity(tokenURL,  null, TokenProperties.class);
        HttpStatus status = response.getStatusCode();
        
        log.info("Status: "+status);
        log.info("Response: "+response.toString());

        return response.toString();
    }

    

}

POJO class:

@JsonIgnoreProperties(ignoreUnknown = true)  
public class TokenProperties {    
    String access_token;
    String token_type;
    String expires_in;
    String scope;
    public String getAccess_token()
    {
        return access_token;
    }

    public void setAccess_token(String access_token)
    {
        this.access_token = access_token;
    }

    public String getToken_type()
    {
        return token_type;
    }

    public void setToken_type(String token_type)
    {
        this.token_type = token_type;
    }

    public String getExpires_in()
    {
        return expires_in;
    }
    public void setExpires_in(String expires_in)
    {
        this.expires_in = expires_in;
    }

    public String getScope()
    {
        return scope;
    }

    public void setScope(String scope)
    {
        this.scope = scope;
    }

    @Override
    public String toString() {
        return "{" + "access_token='" + access_token + '\'' + ", token_type=" + token_type + ", expires_in=" + expires_in + '\'' + "scope='" + scope + '}';
    }

}

Can anyone please help me out? Please let me know where the mistake is. Thanks in Advance!



from Recent Questions - Stack Overflow https://ift.tt/2NJtJA5
https://ift.tt/eA8V8J

No comments:

Post a Comment