spring 为什么我的@RequestHeader不传递值?

mnemlml8  于 11个月前  发布在  Spring
关注(0)|答案(2)|浏览(146)

我在实习期间正在使用springwebmvc REST API作为一项任务。我遇到了一个问题,无法使用Http Header传递值。我试图使用用户名和密码进行身份验证,但authdto值没有在我的应用程序中初始化。
方法:

@GetMapping(value = "/{username}")
    @ApiOperation(value = "fetch Trainee", response = TraineeResponseDTO.class)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Successfully fetched a trainee"),
            @ApiResponse(code = 500, message = "Application failed to process the request")
    })
    public ResponseEntity<TraineeResponseDTO> getTrainee(@PathVariable String username,
                                                         @RequestHeader(value = "Auth-Username") String authUsername,
                                                         @RequestHeader(value = "Auth-Password") String authPassword) {
        traineeValidation.validateUsernameNotNull(username);
        AuthDTO authDTO = new AuthDTO(authUsername, authPassword);
        TraineeEntity trainee = traineeService.findByUsername(authDTO, username);
        TraineeResponseDTO responseDTO = traineeConverter.convertModelToResponse(trainee);
        return new ResponseEntity<>(responseDTO, HttpStatus.OK);
    }

字符串
build.gradle

plugins {
    id 'java'
    id 'jacoco'
    id "org.sonarqube" version "4.4.1.3373"
    id 'war'
}

group = 'com.nikolabojanic'

java {
    sourceCompatibility = '17'
}

sonar {
    properties {
        property "sonar.projectKey", "spring-rest"
        property "sonar.projectName", "spring-rest"
        property "sonar.host.url", "http://localhost:9002"
        property "sonar.token", ""
        property "sonar.jacoco.reportPaths", "build/reports/jacoco/test/jacocoTestReport.xml"
        property "sonar.login", "admin"
        property "sonar.password", "sonar"
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.hibernate:hibernate-core:6.4.0.Final'
    implementation 'org.springframework:spring-orm:6.1.1'
    implementation 'org.postgresql:postgresql:42.7.1'
    implementation 'org.springframework:spring-webmvc:6.1.2'
    implementation 'org.springframework:spring-web:6.1.2'
    implementation 'jakarta.servlet:jakarta.servlet-api:6.0.0'
    implementation 'javax.servlet:javax.servlet-api:4.0.1'
    implementation 'javax.annotation:javax.annotation-api:1.3.2'
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.16.0'
    implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.16.0'
    implementation 'io.springfox:springfox-swagger2:3.0.0'
    implementation 'io.springfox:springfox-swagger-ui:3.0.0'
    implementation 'org.slf4j:slf4j-api:1.7.32'
    implementation 'org.slf4j:slf4j-simple:1.7.32'
    implementation 'org.junit.jupiter:junit-jupiter-engine:5.10.1'
    compileOnly 'org.projectlombok:lombok:1.18.30'
    annotationProcessor 'org.projectlombok:lombok:1.18.30'
    testCompileOnly 'org.projectlombok:lombok:1.18.30'
    testAnnotationProcessor 'org.projectlombok:lombok:1.18.30'
    testImplementation 'org.springframework:spring-test:6.1.0'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.1'
    testImplementation 'org.mockito:mockito-core:5.7.0'
    testImplementation 'org.mockito:mockito-junit-jupiter:5.7.0'
    testImplementation 'org.apache.commons:commons-lang3:3.14.0'
}

test {
    useJUnitPlatform()
    finalizedBy jacocoTestReport
}

jacocoTestReport {
    dependsOn test
    reports {
        xml.required = true
    }
}


enter image description here我从头文件中硬编码了相同的用户名和密码值,以在应用程序中初始化我的authDTO,我的身份验证逻辑工作正常。我认为问题在于我根本没有从头文件中阅读值。

biswetbf

biswetbf1#

你试过写@RequestHeader("Auth-Username")而不是@RequestHeader(value = "Auth-Username")吗?
或许会有帮助。
也看看this topic,也许它会有用。

3vpjnl9f

3vpjnl9f2#

按照spring documentation:你可以这样写请求头注解:

public ResponseEntity<TraineeResponseDTO> getTrainee(@PathVariable String username,
                                                         @RequestHeader("Auth-Username") String authUsername,
                                                         @RequestHeader("Auth-Password") String authPassword) {

字符串
它将解决这个问题。快乐编码...!!:)

相关问题