You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot中验证GitLab私有令牌及gitlab4j-api依赖报错解决

GitLab私有令牌验证:gitlab4j-api依赖冲突问题解决

问题概述

在验证GitLab私有令牌有效性时,使用gitlab4j-api Maven依赖先后出现两个错误:

  1. java.lang.ClassNotFoundException: javax.ws.rs.core.StreamingOutput
  2. java.lang.VerifyError,具体类型不匹配错误:org/glassfish/jersey/client/JerseyClientBuilder无法转换为javax/ws/rs/client/ClientBuilder

代码示例

@PostMapping("/validate-token")
public void validateToken(@RequestParam String token, @RequestBody Object json) {
    System.out.printf("json " + json);
    System.out.println("Token " + token);

    //TODO need to fix error on below line.
    GitLabApi gitLabApi = new GitLabApi("https://gitlab.com", token);

    System.out.println("gitLabApi " + gitLabApi.toString());
    UserApi userApi = gitLabApi.getUserApi();
    System.out.println("UserApi " + userApi);
    if (userApi != null) {
        System.out.println("token is valid");
    } else {
        System.out.println("token is not valid");
    }
}

错误堆栈信息

Exception Details:
  Location:
    org/gitlab4j/api/GitLabApiClient.createApiClient()Ljavax/ws/rs/client/Client; @18: invokevirtual
  Reason:
    Type 'org/glassfish/jersey/client/JerseyClientBuilder' (current frame, stack[0]) is not assignable to 'javax/ws/rs/client/ClientBuilder'
  Current Frame:
    bci: @18
    flags: { }
    locals: { 'org/gitlab4j/api/GitLabApiClient', 'org/glassfish/jersey/client/JerseyClientBuilder' }
    stack: { 'org/glassfish/jersey/client/JerseyClientBuilder', 'java/lang/Class' }
  Bytecode:
    0000000: bb00 7d59 b700 7e2a b400 1ab6 007f 4c2b
    0000010: 122d b600 8057 2b12 2fb6 0080 572a b400
    0000020: 8199 0013 2b2a b400 82b6 0083 2ab4 0084
    0000030: b600 8557 2a2b b600 86b5 0031 2ab4 0031
    0000040: b0                                      
  Stackmap Table:
    append_frame(@52,Object[#374])
] with root cause

java.lang.VerifyError: Bad type on operand stack
Exception Details:
  Location:
    org/gitlab4j/api/GitLabApiClient.createApiClient()Ljavax/ws/rs/client/Client; @18: invokevirtual
  Reason:
    Type 'org/glassfish/jersey/client/JerseyClientBuilder' (current frame, stack[0]) is not assignable to 'javax/ws/rs/client/ClientBuilder'
  Current Frame:
    bci: @18
    flags: { }
    locals: { 'org/gitlab4j/api/GitLabApiClient', 'org/glassfish/jersey/client/JerseyClientBuilder' }
    stack: { 'org/glassfish/jersey/client/JerseyClientBuilder', 'java/lang/Class' }
  Bytecode:

解决方案

1. 解决依赖冲突问题

这类错误本质是JAX-RS API与Jersey客户端实现版本不兼容,或项目中存在多份JAX-RS相关依赖导致冲突,处理方式如下:

  • 匹配gitlab4j-api与项目的EE版本:
    • gitlab4j-api v5.x及以上版本使用Jakarta EE API(包路径为jakarta.ws.rs),对应Spring Boot 3.x或Jakarta EE环境;
    • gitlab4j-api v4.x版本使用传统Java EE API(包路径为javax.ws.rs),对应Spring Boot 2.x或Java EE环境。
      根据项目环境选择对应版本,避免跨版本混用。
  • 清理冲突依赖:
    在Maven的pom.xml中,检查是否存在其他JAX-RS/Jersey相关依赖(如javax.ws.rs-api、jersey-client),如果有,要么统一版本,要么通过<exclusions>排除冲突依赖。例如:
    <dependency>
        <groupId>org.gitlab4j</groupId>
        <artifactId>gitlab4j-api</artifactId>
        <version>5.3.0</version> <!-- 适配Jakarta EE环境 -->
        <!-- 若存在冲突,排除多余依赖 -->
        <exclusions>
            <exclusion>
                <groupId>jakarta.ws.rs</groupId>
                <artifactId>jakarta.ws.rs-api</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    

2. 修复令牌验证逻辑

原代码中仅判断userApi != null无法验证令牌有效性,因为getUserApi()只是获取API实例,不会触发GitLab请求。正确做法是调用实际API接口(如获取当前用户信息),通过捕获异常判断令牌是否有效:

import org.gitlab4j.api.GitLabApi;
import org.gitlab4j.api.GitLabApiException;
import org.gitlab4j.api.models.User;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TokenValidationController {

    @PostMapping("/validate-token")
    public ResponseEntity<String> validateToken(@RequestParam String token) {
        System.out.println("待验证令牌: " + token);
        GitLabApi gitLabApi = new GitLabApi("https://gitlab.com", token);
        
        try {
            // 调用GitLab API获取当前用户,验证令牌有效性
            User currentUser = gitLabApi.getUserApi().getCurrentUser();
            System.out.println("令牌有效,关联用户: " + currentUser.getUsername());
            return ResponseEntity.ok("令牌有效");
        } catch (GitLabApiException e) {
            System.err.println("令牌验证失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("令牌无效");
        }
    }
}

内容的提问来源于stack exchange,提问作者Onic Team

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 08:31:09