如何在spring boot 使用 gitlab的Api

发布时间:2022-08-19 14:26

前言

最近有项目需求是通过gitlab的api获取其中的数据。来记录一下我的踩坑的过程。
首先先尝试了获取users数据,即所有用户的数据。

这里是gitlab官方API文档 https://docs.gitlab.com/ee/ap...

这里说一下我刚开始的做法和老师建议的做法。

最开始的做法

1. 获取Admin access token

首先我们需要获取gitlab的Admin access token, 并且在请求的时候附带这个token,gitlab才能认证我们的身份并返回数据。

1.登录 GitLab。 2.登录后,点击右上角的头像图标然后选择 Preferences。

3.在 access token 界面就可以新建token了。

当你是group的管理员之后,就可以通过该token获取users了。

2.使用spring boot单元测试发起请求

这里我使用了apache发起http请求,并使用了fastjson处理请求后所返回的json数据。


  org.apache.httpcomponents
  httpclient
  4.5.9


 
    com.alibaba
    fastjson
    1.2.76

以下是我测试的代码。来解释一下:

  1. 首先定义你的要请求的url,格式如代码。也可以看官方文档。
  2. 构建Http请求类,并设置参数。参数必须要有private_token。这里我加的 without_project_bots可以过滤机器人user。
  3. 使用execute函数发起get请求,并接收返回的json数据。
  4. 从json数组转换成实体数组

这样就获得了我们需要的数据。

@Test
  void getUsers() throws IOException, URISyntaxException {

    String url = "https://your gitlab address/api/v4/users";


    CloseableHttpClient httpclients = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet(url);
    // 设置参数信息
    URI uri = new URIBuilder(httpGet.getURI())
            .addParameter("private_token", "your amind token")
            .addParameter("without_project_bots", "true")
            .build();
    ((HttpRequestBase) httpGet).setURI(uri);

    // 发起请求
    CloseableHttpResponse response = httpclients.execute(httpGet);

    // 处理数据
    HttpEntity httpEntity = (HttpEntity) response.getEntity();
    String result =  EntityUtils.toString(httpEntity,"UTF-8");

    // 从json转换成实体
    final ObjectMapper objectMapper = new ObjectMapper();
    User[] langList = objectMapper.readValue(result, User[].class);

    logger.debug(String.valueOf(langList));

    response.close();
  }

期间我遇到的问题

1. java需要添加证书

由于我们发起的请求地址是HTTPS的,而https是对链接加了安全证书SSL的,如果服务器中没有相关链接的SSL证书,它就不能够信任那个链接, 就会报sun.security.validator.ValidatorException错误。

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

最开始我是把证书添加到本地中的,添加方法大概参考了如下博客
https://www.cnblogs.com/zoro-...

这样就把证书添加到了本地的java密钥库中。

但是这样只有本地能够正常使用这个项目。因为在并没有把证书添加到项目中。所以这也说明了这种方法并不普适。

所以我们需要把证书放在src/main/resourse中,部署https。下面我会说一下如何添加。

2. 没有配置数据源

Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]

image.png

由于我只是在单元测试中测试,项目刚初始化,并没有连接数据库,也就是并没有配置数据源。

所以因为没有向 Spring Boot 传递足够的数据来配置数据源,导致发生了该报错。

所以,如果我们暂时不需要数据源,就可以使用exclude={DataSourceAutoConfiguration.class}自动配置。 这样就可以解决报错

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
public class NonlycodeApplication {
}

3. json数据的属性多于实体的属性

在我请求到数据后,可以看到gitlab给我们返回来的数据有很多。有theme_id, note,is_admin等信息。
如何在spring boot 使用 gitlab的Api_第1张图片


但是, 我定义的实体中目前只定义了几条需要的信息,比如id ,name,username等信息。

public class User implements Serializable {

  private static final long serialVersionUID = 1L;

  @Id
  private Long id;

  private String email = "";

  private Integer following = 0;

  private Integer followers = 0;

  private String name = "";

  private  String password = "yunzhi";

  @Column(nullable = false, unique = true)
  private String username;

在json转化数据的时候,遇到这种情况就会报错。并且报错说:not marked as ignorable。
image.png

所以我们需要添加注释,把不需要的数据给忽略掉。

我的解决方法是添加 @JsonIgnoreProperties(ignoreUnknown = true)
最后报错消失。

@JsonIgnoreProperties(ignoreUnknown = true)
@Entity
public class User implements Serializable {

使用RestTemplate通过证书认证访问https

传统情况下在java代码里访问restful服务,一般使用Apache的HttpClient。如同我上面的代码。不过此种方法使用起来太过繁琐。spring提供了一种简单便捷的模板类来进行操作,这就是RestTemplate。

1. 获取证书

我参考了这篇文章来生成自签名证书。
https://www.baeldung.com/spri...

或者可以下载网站,参考这篇文章来创建密钥库
https://fullstackdeveloper.gu...

把生成的文件导入到项目resourses/keystore中
如何在spring boot 使用 gitlab的Api_第2张图片

2. 配置application.yml

填写一下自己配置的password和alias别名等

server:
  ssl:
    key-store-type: PKCS12
    key-store: classpath:keystore/baeldung.p12
    key-store-password: changeit
    key-alias: baeldung
  port: 8443

spring:
  security:
    user:
      name: root
      password: 1234

4. 配置restTemplate

配置restTemplate,使其加载项目中的证书

  1. 新建CustomConfiguration类
    如何在spring boot 使用 gitlab的Api_第3张图片
  2. 重写restTemplate()函数

注意一下自己的文件名,以及刚才自己设置的key-store密码

@Configuration
public class CustomConfiguration {

  @Bean
  RestTemplate restTemplate() throws Exception {
    SSLContext sslContext = new SSLContextBuilder()
            .loadTrustMaterial(new URL("file:src/main/resources/keystore/baeldung.p12"), "changeit".toCharArray()).build();
    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext);
    HttpClient httpClient = HttpClients.custom()
            .setSSLSocketFactory(socketFactory)
            .build();
    HttpComponentsClientHttpRequestFactory factory =
            new HttpComponentsClientHttpRequestFactory(httpClient);
    return new RestTemplate(factory);
  }
}

3. 编写代码

controller层:

@RestController
@RequestMapping("user")
public class UserController {

  @Autowired
  UserService userService;


  @GetMapping("/getUsers")
  @JsonView(GetUsersJsonView.class)
  public User[] getUsers() throws JsonProcessingException {
    return  this.userService.getUsers();
  }

  public class GetUsersJsonView {
  }
}

service层:
注意填写自己的url和token。 逻辑跟之前的代码差不多。

@Override
  public User[] getUsers() throws JsonProcessingException {
    String url = "https://your gitlab address /api/v4/users/" +
            "?private_token={private_token}&without_project_bots={without_project_bots}";

    final Map variables = new HashMap<>();

    variables.put("private_token", "your token");
    variables.put("without_project_bots", "true");

    RestTemplate restTemplate = new RestTemplate();
    String response = restTemplate.getForObject(url, String.class, variables);


    // 从json转换成实体
    final ObjectMapper objectMapper = new ObjectMapper();
    User[] userList = objectMapper.readValue(response, User[].class);

    return userList;
  }

4. url访问

image.png

可能是因为spring security, 需要登录。 我们输入application.yml中配置好的账号密码
如何在spring boot 使用 gitlab的Api_第4张图片

4. 结果

可能是jsonView没配置好,页面显示空信息。之后再弄一下。
如何在spring boot 使用 gitlab的Api_第5张图片

但是经过打断点打印, 显示成果获取到了gitlab中的users信息。
如何在spring boot 使用 gitlab的Api_第6张图片

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号