gpt4 book ai didi

java - 如何从 Micronaut 中的 Controller 调用另一个 Rest api

转载 作者:行者123 更新时间:2023-12-02 09:29:06 25 4
gpt4 key购买 nike

从此artcle ,我已经实现了从 micronaut gradle 应用程序中的 REST API 方法调用另一个 REST API。由于我的 REST API 需要 jwt token ,因此我发送的 token 与我在当前请求中收到的 token 相同。即使正在传递 token ,我也看到未经授权错误。任何人都可以在这方面提供帮助。下面是我的代码。

import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpStatus;
import io.appter.clientmgmt.models.ClientContact;
import io.appter.clientmgmt.repositories.IClientContactRepository;
import io.micronaut.http.uri.UriTemplate;
import io.micronaut.security.annotation.Secured;
import io.micronaut.security.authentication.Authentication;
import io.micronaut.security.rules.SecurityRule;
import io.micronaut.http.annotation.*;
import io.micronaut.http.client.RxHttpClient;
import io.micronaut.http.client.annotation.Client;
import io.reactivex.Flowable;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;

import javax.validation.constraints.NotNull;
import java.security.Security;
import java.util.List;


@Controller("/clientcontact")
//@Secured(SecurityRule.IS_ANONYMOUS)
public class ClientContactController {
private static final Logger LOG = LoggerFactory.getLogger(ClientContactController.class);
private IClientContactRepository clientContactRepository;
private final RxHttpClient httpClient;

public ClientContactController(IClientContactRepository clientContactRepository,
@Client("http://appterauthsvc-env.g2yapp2kcp.us-east-1.elasticbeanstalk.com") RxHttpClient httpClient) {
this.clientContactRepository = clientContactRepository;
this.httpClient = httpClient;
}

@Get("/")
public HttpStatus index() {
return HttpStatus.OK;
}

@Post("/")
@Secured(SecurityRule.IS_AUTHENTICATED)
public ClientContact createClientContact(@Body ClientContact clientContact,
Authentication authentication,
@Header("Authorization") String authorization) {
try {
List<ClientContact> existingClientContacts = clientContactRepository.getClientContactByClientId(clientContact.getClientId());
LOG.info("current contacts count for the client " + clientContact.getClientId() + " is " + existingClientContacts.size());
if (existingClientContacts.isEmpty()) {

User userObj = new User();
Long clientId = new Long(clientContact.getClientId());
userObj.setClientId(clientId);
userObj.setFirstName(clientContact.getFirstName());
userObj.setLastName(clientContact.getLastName());
userObj.setEmailId(clientContact.getEmailAddress());
userObj.setPhoneNo(clientContact.getContactNumber());
userObj.setIsActive(true);

LOG.info("User Email set is: "+userObj.getEmailId());
LOG.info("authorization token is: "+authorization);
HttpRequest<?> request = HttpRequest.POST("/user", userObj).bearerAuth(authorization);

String response = httpClient.toBlocking().retrieve(request);
LOG.info("Request Object: "+ request.toString());
LOG.info("Response Object: "+ response.toString());
LOG.info("User API executed.. ");
}


return clientContactRepository.createClientContact(clientContact);
} catch (Exception ex) {
LOG.error(ex.getMessage(), ex);
return null;
}
}
}

提前致谢。

最佳答案

可能是因为 @Header("Authorization") 字符串授权 返回类似 Bearer xyz... 的内容,并且 bearerAuth 方法正在添加Bearer 到字符串,以便您发送 Bearer Bearer xyz...

所以只需执行.header(HttpHeaders.AUTHORIZATION, 授权)

此外,作为旁注,您确实不应该在此方法中执行阻塞 HTTP 调用。这并不是世界末日,因为在这种情况下您会阻塞 IO 线程,但是应该避免这种类型的代码。

关于java - 如何从 Micronaut 中的 Controller 调用另一个 Rest api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58117776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com