简体   繁体   中英

Make a post request from one api to another using spring

I have an API that logs in the user with some credentials and I am developing another API that register the logs of the program.

Basically I want to make a call to another api on my method.

This is the code that I have right now but I always get 500 null error:

This is the login method on my controller:

@RequestMapping(value = "/authenticate", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Object authenticate(@RequestBody UserEntity user) {

    logger.debug("Begin request UaaController.authenticate()");

    try {
        this.redirectLogs("log that I want to process", logs_url);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    UserEntity usr = authenticationService.authenticate(user.getName(), user.getPassword().getPassword());

    if (usr == null) {
        logger.debug("User could not be found when executing UaaController.authenticate()");
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    } else {
        logger.debug("Executed UaaController.authenticate() successfully");
        return UaaService.getPermissionsFromUser(usr);
    }

}

When the user tries to authenticate himself this method this.redirectLogs should send the message to my log api and from them create a log.

This is the method:

private Object redirectLogs(String body,String logs_url) throws IOException {


    StringBuilder redirectUrl = new StringBuilder();
    redirectUrl.append(logs_url);


    RestTemplate restTemplate = new RestTemplate();

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<String> entity = new HttpEntity<>(body, headers);

    String response= restTemplate.postForObject(redirectUrl.toString(), entity, String.class);

    return response;

}

The logs url is the path the method has on my loggerAPI:

interservice.logs.endpoint=http://localhost:8084/logs/success

And here is my controller for the log:

@RestController("/logs")
public class Log {

private static final Logger LOG = Logger.getLogger(Log.class.getName());


@PostMapping("/success")
public String helloWorld() {
    String response = "Welcome to javainuse " + new Date();
    LOG.log(Level.INFO, response);

    return response;
}

}

When Im debuging the program stops when Im doing the call to restTemplate.postForObject call.

Observation:The String response = "Welcome to javainuse " + new Date(); on my log method is just for test, what I want is to present the message on the body of the post request but now I just want the connection between API to work.

Try this.

private Object redirectLogs(String body) {
   return Jsoup.connect("http://localhost:8084/logs/success")
            .method(Connection.Method.POST)
            .header("Content-Type", "application/json")
            .requestBody(body)
            .execute();
}

<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.8.3</version>
</dependency>

And check your second controller with the postman

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM