简体   繁体   English

使用Spring向一个API发出发布请求

[英]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. 我有一个使用某些凭据登录用户的API,并且我正在开发另一个注册程序日志的API。

Basically I want to make a call to another api on my method. 基本上,我想在我的方法上调用另一个api。

This is the code that I have right now but I always get 500 null error: 这是我现在拥有的代码,但是我总是得到500空错误:

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.redirectLogs应该将消息发送到我的日志api,并从中创建日志。

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: 日志网址是该方法在我的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. 当我调试时,当我执行对restTemplate.postForObject的调用时,程序停止。

Observation:The String response = "Welcome to javainuse " + new Date(); 观察:String响应=“ 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. 在我的log方法上仅用于测试,我想要的是在发布请求的正文上显示消息,但是现在我只希望API之间的连接正常工作。

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 并与邮递员检查您的第二个控制器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 POST 请求从一个控制器重定向到另一个控制器 Spring Boot - Redirect to a POST request from one controller to another controller Spring Boot 在Spring上从另一个站点接收POST请求 - Receiving a POST Request on Spring From Another Site 从一台服务器到另一台服务器的POST请求 - POST Request from one server to another 如何在Spring Boot中将POST请求从一个Web应用程序正确转发到另一个Web应用程序? - How to properly forward POST request from one web application to another in Spring Boot? 通过REST API请求从一台服务器向另一台服务器发布数据库中POST数据的正确方法? - Correct way to POST data in database through REST API request from one server to another? 从一个容器向另一个容器发出 HTTP 请求 - Make HTTP request from one container to another spring JPA 在 API 请求中发布 - spring JPA to post in API request 如何从前端向Spring发出HTTP POST请求? - How to make HTTP POST request from front-end to Spring? 如何使用JAVA REST API将请求从应用程序连续发布到另一个应用程序 - How to POST REQUEST using JAVA REST API from application to another consecutively 我想从一个 API 响应正文中获取价值,并使用 Cucumber gherkin 将其用于另一个 api 请求 - I want to take value from one API response body and use into another api request using Cucumber gherkin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM