【发布时间】:2019-04-18 13:30:10
【问题描述】:
我有一个使用一些凭据登录用户的 API,我正在开发另一个注册程序日志的 API。
基本上我想在我的方法上调用另一个 api。
这是我现在拥有的代码,但我总是收到 500 null 错误:
这是我控制器上的登录方法:
@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);
}
}
当用户尝试对自己进行身份验证时,此方法 this.redirectLogs 应该将消息发送到我的日志 api 并从中创建一个日志。
这是方法:
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;
}
日志 url 是该方法在我的 loggerAPI 上的路径:
interservice.logs.endpoint=http://localhost:8084/logs/success
这是我的日志控制器:
@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;
}
}
当我调用 restTemplate.postForObject 调用时,当我调试程序停止时。
观察:String response = "Welcome to javainuse" + new Date();我的 log 方法只是为了测试,我想要的是在 post 请求的正文上显示消息,但现在我只希望 API 之间的连接正常工作。
【问题讨论】:
标签: java spring spring-boot