[英]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.