I have migrated my Spring Boot application to a native image using GraalVM, but I’m encountering an issue where objects are returning null in HTTP POST and HTTP PUT responses. The requests successfully return 201
or 200
status codes, but the response body is empty.
For example, here’s my authenticate method which should return an access_token:
@Override
public ResponseObject authenticate(AuthenticationRequest request) {
log.info("userAuthentication with email: {}", request.getEmail());
if(userRepository.findByEmail(request.getEmail()).isPresent()) {
if (!userRepository.findByEmailAndDeletedAtIsNull(request.getEmail()).isPresent()) {
log.error("User with email is not active: {}", request.getEmail());
return new ResponseObject(HttpStatus.NOT_FOUND.value(),
"User with email is not active: " + request.getEmail());
}
authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(
request.getEmail(),
request.getPassword()
)
);
UserEntity user = userRepository.findByEmail(request.getEmail())
.orElseThrow();
String jwtToken = jwtService.generateToken(user);
String refreshToken = jwtService.generateRefreshToken(user);
revokeAllUserTokens(user);
saveUserToken(user, jwtToken);
AuthenticationResponse authenticationResponse = new AuthenticationResponse();
authenticationResponse.setAccessToken(jwtToken);
authenticationResponse.setRefreshToken(refreshToken);
ResponseObject responseObject = new ResponseObject();
responseObject.setStatus(HttpStatus.OK.value());
responseObject.setData(authenticationResponse);
return responseObject;
} else {
log.error("User with email: {} does not exist!", request.getEmail());
return new ResponseObject(HttpStatus.NOT_FOUND.value(),
"User with email: " + request.getEmail() + " does not found!");
}
}
When I execute the .jar file, everything works perfectly, but with the native image, the response is:
{
"status": 200,
"data": {}
}
Here are the ResponseObject and AuthenticationResponse classes:
package com.emailcampaigns.campaignserviceapi.response;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class ResponseObject<T> {
private Integer status;
private T data;
}
package com.emailcampaigns.campaignserviceapi.auth;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.io.Serializable;
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class AuthenticationResponse implements Serializable {
@JsonProperty("access_token")
private String accessToken;
@JsonProperty("refresh_token")
private String refreshToken;
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public String getRefreshToken() {
return refreshToken;
}
public void setRefreshToken(String refreshToken) {
this.refreshToken = refreshToken;
}
}
Here is build.gradle configuration:
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.5'
id 'io.spring.dependency-management' version '1.1.4'
id 'org.hibernate.orm' version '6.4.10.Final'
id 'org.graalvm.buildtools.native' version '0.9.28'
}
group = 'com.email-campaigns'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '17'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'io.jsonwebtoken:jjwt-api:0.11.5'
implementation 'io.jsonwebtoken:jjwt-impl:0.11.5'
implementation 'io.jsonwebtoken:jjwt-jackson:0.11.5'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.1.0'
implementation 'org.springframework.boot:spring-boot-starter-validation'
runtimeOnly 'org.postgresql:postgresql'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign:4.1.0'
implementation 'org.springframework.boot:spring-boot-starter-amqp'
implementation 'org.springframework.boot:spring-boot-starter-websocket:3.3.2'
}
tasks.named('test') {
useJUnitPlatform()
}
Additionally, when I try to make an HTTP GET request, I encounter the following error:
org.springframework.orm.jpa.JpaSystemException: Generation of HibernateProxy instances at runtime is not allowed when the configured BytecodeProvider is ‘none’; your model requires a more advanced BytecodeProvider to be enabled.
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:341) ~[na:na]
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:241) ~[na:na]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:550) ~[campaign-service-api.exe:6.1.6]
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61) ~[na:na]
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:335) ~[na:na]
Has anyone experienced a similar issue or can provide insight into why this is happening with the native image? Any guidance on how to resolve this would be greatly appreciated.
2