spring-projects / spring-boot

Spring Boot
https://spring.io/projects/spring-boot
Apache License 2.0
74.54k stars 40.54k forks source link

Spring Boot 3 behaves differently JAR versus mvn spring-boot:run #36951

Closed robozb closed 1 year ago

robozb commented 1 year ago

Dear All,

I don't know why but Spring Boot behaves differently when I package it into a JAR versus when I start it with mvn spring-boot:run.

Spring serializes the same Exception of same Rest method on different way.

Two cases: Run with: mvn spring-boot:run: I see the trace and message fields. Run the JAR with: ...java.exe -jar .\vsm.jar: The "trace and message fields" are dissapeared.

I made a short video about the problem: https://www.youtube.com/watch?v=jv_eQONkyWY&ab_channel=B%C3%A9ciRoboz

Thank you so much for any help or advice!

Kind Regards:

Bela

Dependencies:

hu.wfs.vau:vsm-backend:jar:0.0.11-SNAPSHOT
+- org.springframework.boot:spring-boot-starter-web:jar:3.0.9:compile
|  +- org.springframework.boot:spring-boot-starter:jar:3.0.9:compile
|  |  +- jakarta.annotation:jakarta.annotation-api:jar:2.1.1:compile
|  |  \- org.yaml:snakeyaml:jar:1.33:compile
|  +- org.springframework.boot:spring-boot-starter-json:jar:3.0.9:compile
|  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.14.3:compile
|  |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.14.3:compile
|  +- org.springframework.boot:spring-boot-starter-tomcat:jar:3.0.9:compile
|  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:10.1.11:compile
|  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:10.1.11:compile
|  +- org.springframework:spring-web:jar:6.0.11:compile
|  |  \- org.springframework:spring-beans:jar:6.0.11:compile
|  \- org.springframework:spring-webmvc:jar:6.0.11:compile
|     +- org.springframework:spring-context:jar:6.0.11:compile
|     \- org.springframework:spring-expression:jar:6.0.11:compile
+- org.springframework.boot:spring-boot-devtools:jar:3.0.9:runtime
|  +- org.springframework.boot:spring-boot:jar:3.0.9:compile
|  \- org.springframework.boot:spring-boot-autoconfigure:jar:3.0.9:compile
+- org.springframework.boot:spring-boot-starter-test:jar:3.0.9:test
|  +- org.springframework.boot:spring-boot-test:jar:3.0.9:test
|  +- org.springframework.boot:spring-boot-test-autoconfigure:jar:3.0.9:test
|  +- com.jayway.jsonpath:json-path:jar:2.7.0:test
|  |  \- net.minidev:json-smart:jar:2.4.11:test
|  |     \- net.minidev:accessors-smart:jar:2.4.11:test
|  |        \- org.ow2.asm:asm:jar:9.3:compile
|  +- jakarta.xml.bind:jakarta.xml.bind-api:jar:4.0.0:compile
|  |  \- jakarta.activation:jakarta.activation-api:jar:2.1.2:compile
|  +- org.assertj:assertj-core:jar:3.23.1:test
|  |  \- net.bytebuddy:byte-buddy:jar:1.12.23:runtime
|  +- org.hamcrest:hamcrest:jar:2.2:test
|  +- org.junit.jupiter:junit-jupiter:jar:5.9.3:test
|  |  +- org.junit.jupiter:junit-jupiter-api:jar:5.9.3:test
|  |  |  +- org.opentest4j:opentest4j:jar:1.2.0:test
|  |  |  +- org.junit.platform:junit-platform-commons:jar:1.9.3:test
|  |  |  \- org.apiguardian:apiguardian-api:jar:1.1.2:test
|  |  +- org.junit.jupiter:junit-jupiter-params:jar:5.9.3:test
|  |  \- org.junit.jupiter:junit-jupiter-engine:jar:5.9.3:test
|  |     \- org.junit.platform:junit-platform-engine:jar:1.9.3:test
|  +- org.mockito:mockito-core:jar:4.8.1:test
|  |  +- net.bytebuddy:byte-buddy-agent:jar:1.12.23:test
|  |  \- org.objenesis:objenesis:jar:3.2:test
|  +- org.mockito:mockito-junit-jupiter:jar:4.8.1:test
|  +- org.skyscreamer:jsonassert:jar:1.5.1:test
|  |  \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
|  +- org.springframework:spring-core:jar:6.0.11:compile
|  |  \- org.springframework:spring-jcl:jar:6.0.11:compile
|  +- org.springframework:spring-test:jar:6.0.11:test
|  \- org.xmlunit:xmlunit-core:jar:2.9.1:test
+- org.springframework.boot:spring-boot-starter-log4j2:jar:3.0.9:compile
|  +- org.apache.logging.log4j:log4j-slf4j2-impl:jar:2.19.0:compile
|  |  \- org.apache.logging.log4j:log4j-api:jar:2.19.0:compile
|  +- org.apache.logging.log4j:log4j-core:jar:2.19.0:compile
|  \- org.apache.logging.log4j:log4j-jul:jar:2.19.0:compile
+- org.slf4j:log4j-over-slf4j:jar:2.0.7:compile
|  \- org.slf4j:slf4j-api:jar:2.0.7:compile
+- net.bull.javamelody:javamelody-spring-boot-starter:jar:2.0.0:compile
|  +- net.bull.javamelody:javamelody-core:jar:2.0.0:compile
|  |  \- org.jrobin:jrobin:jar:1.5.9:compile
|  \- org.springframework.boot:spring-boot-starter-aop:jar:3.0.9:compile
|     \- org.aspectj:aspectjweaver:jar:1.9.19:compile
+- org.springframework.boot:spring-boot-starter-data-jpa:jar:3.0.9:compile
|  +- org.springframework.boot:spring-boot-starter-jdbc:jar:3.0.9:compile
|  |  +- com.zaxxer:HikariCP:jar:5.0.1:compile
|  |  \- org.springframework:spring-jdbc:jar:6.0.11:compile
|  +- org.hibernate.orm:hibernate-core:jar:6.1.7.Final:compile
|  |  +- jakarta.persistence:jakarta.persistence-api:jar:3.1.0:compile
|  |  +- jakarta.transaction:jakarta.transaction-api:jar:2.0.1:compile
|  |  +- org.jboss.logging:jboss-logging:jar:3.5.3.Final:compile
|  |  +- org.hibernate.common:hibernate-commons-annotations:jar:6.0.6.Final:runtime
|  |  +- org.jboss:jandex:jar:2.4.2.Final:runtime
|  |  +- com.fasterxml:classmate:jar:1.5.1:compile
|  |  +- org.glassfish.jaxb:jaxb-runtime:jar:4.0.3:compile
|  |  |  \- org.glassfish.jaxb:jaxb-core:jar:4.0.3:compile
|  |  |     +- org.eclipse.angus:angus-activation:jar:2.0.1:runtime
|  |  |     +- org.glassfish.jaxb:txw2:jar:4.0.3:compile
|  |  |     \- com.sun.istack:istack-commons-runtime:jar:4.1.2:compile
|  |  +- jakarta.inject:jakarta.inject-api:jar:2.0.0:runtime
|  |  \- org.antlr:antlr4-runtime:jar:4.10.1:runtime
|  +- org.springframework.data:spring-data-jpa:jar:3.0.8:compile
|  |  +- org.springframework.data:spring-data-commons:jar:3.0.8:compile
|  |  +- org.springframework:spring-orm:jar:6.0.11:compile
|  |  \- org.springframework:spring-tx:jar:6.0.11:compile
|  \- org.springframework:spring-aspects:jar:6.0.11:compile
+- org.springframework.boot:spring-boot-starter-security:jar:3.0.9:compile
|  +- org.springframework:spring-aop:jar:6.0.11:compile
|  +- org.springframework.security:spring-security-config:jar:6.0.5:compile
|  \- org.springframework.security:spring-security-web:jar:6.0.5:compile
+- org.springframework.boot:spring-boot-starter-validation:jar:3.0.9:compile
|  +- org.apache.tomcat.embed:tomcat-embed-el:jar:10.1.11:compile
|  \- org.hibernate.validator:hibernate-validator:jar:8.0.1.Final:compile
|     \- jakarta.validation:jakarta.validation-api:jar:3.0.2:compile
+- org.springframework.security:spring-security-test:jar:6.0.5:test
|  \- org.springframework.security:spring-security-core:jar:6.0.5:compile
|     \- org.springframework.security:spring-security-crypto:jar:6.0.5:compile
+- org.springframework.boot:spring-boot-starter-oauth2-resource-server:jar:3.0.9:compile
|  +- org.springframework.security:spring-security-oauth2-resource-server:jar:6.0.5:compile
|  |  \- org.springframework.security:spring-security-oauth2-core:jar:6.0.5:compile
|  \- org.springframework.security:spring-security-oauth2-jose:jar:6.0.5:compile
|     \- com.nimbusds:nimbus-jose-jwt:jar:9.24.4:compile
|        \- com.github.stephenc.jcip:jcip-annotations:jar:1.0-1:compile
+- org.springframework.boot:spring-boot-starter-thymeleaf:jar:3.0.9:compile
|  \- org.thymeleaf:thymeleaf-spring6:jar:3.1.1.RELEASE:compile
|     \- org.thymeleaf:thymeleaf:jar:3.1.1.RELEASE:compile
|        +- org.attoparser:attoparser:jar:2.0.6.RELEASE:compile
|        \- org.unbescape:unbescape:jar:1.1.6.RELEASE:compile
+- org.thymeleaf.extras:thymeleaf-extras-springsecurity6:jar:3.1.1.RELEASE:compile
+- org.springframework.boot:spring-boot-starter-actuator:jar:3.0.9:compile
|  +- org.springframework.boot:spring-boot-actuator-autoconfigure:jar:3.0.9:compile
|  |  \- org.springframework.boot:spring-boot-actuator:jar:3.0.9:compile
|  +- io.micrometer:micrometer-observation:jar:1.10.9:compile
|  |  \- io.micrometer:micrometer-commons:jar:1.10.9:compile
|  \- io.micrometer:micrometer-core:jar:1.10.9:compile
|     +- org.hdrhistogram:HdrHistogram:jar:2.1.12:runtime
|     \- org.latencyutils:LatencyUtils:jar:2.0.3:runtime
+- com.sun.mail:javax.mail:jar:1.6.2:compile
|  \- javax.activation:activation:jar:1.1:compile
+- org.passay:passay:jar:1.6.3:compile
+- com.sun.activation:jakarta.activation:jar:2.0.0:compile
+- hu.wfs.lib.halozat:WFSLib_Halozat:jar:2.7.0:compile
|  +- hu.wfs.lib.biztonsag:WFSLib_Biztonsag:jar:2.5.3:compile
|  |  +- hu.wfs.lib.altalanos:WFSLib_Altalanos:jar:2.7.2:compile
|  |  |  +- org.apache.velocity:velocity:jar:1.5:compile
|  |  |  |  +- commons-collections:commons-collections:jar:3.1:compile
|  |  |  |  +- commons-lang:commons-lang:jar:2.1:compile
|  |  |  |  \- oro:oro:jar:2.0.8:compile
|  |  |  +- net.sourceforge.jexcelapi:jxl:jar:2.6.12:compile
|  |  |  +- org.apache.poi:poi:jar:3.17:compile
|  |  |  |  \- org.apache.commons:commons-collections4:jar:4.1:compile
|  |  |  +- org.apache.poi:poi-ooxml:jar:3.17:compile
|  |  |  |  +- org.apache.poi:poi-ooxml-schemas:jar:3.17:compile
|  |  |  |  |  \- org.apache.xmlbeans:xmlbeans:jar:2.6.0:compile
|  |  |  |  |     \- stax:stax-api:jar:1.0.1:compile
|  |  |  |  \- com.github.virtuald:curvesapi:jar:1.04:compile
|  |  |  +- com.sun.xml.bind:jaxb-core:jar:4.0.3:compile
|  |  |  \- com.sun.xml.bind:jaxb-impl:jar:4.0.3:compile
|  |  +- kulsojar.bcprov_jdk16_jeloletlen_1_43:bcprov_jdk16_jeloletlen_1_43:jar:1.0.0:compile
|  |  +- ch.ethz.ganymed:ganymed-ssh2:jar:build210:compile
|  |  +- J2SSHCommon:J2SSHCommon:jar:0.2.9-patched:system
|  |  +- J2SSHCore:J2SSHCore:jar:0.2.9-patched:system
|  |  +- commons-httpclient:commons-httpclient:jar:3.0.1:compile
|  |  \- commons-logging:commons-logging:jar:1.0.4:compile
|  +- javax.mail:javax.mail-api:jar:1.6.2:compile
|  +- javax.xml.ws:jaxws-api:jar:2.3.1:compile
|  |  +- javax.xml.bind:jaxb-api:jar:2.3.1:compile
|  |  |  \- javax.activation:javax.activation-api:jar:1.2.0:compile
|  |  +- javax.xml.soap:javax.xml.soap-api:jar:1.4.0:compile
|  |  \- javax.annotation:javax.annotation-api:jar:1.3.2:compile
|  \- org.jsoup:jsoup:jar:1.13.1:compile
+- org.projectlombok:lombok:jar:1.18.28:compile
+- org.keycloak:keycloak-admin-client:jar:21.0.1:compile
|  +- org.keycloak:keycloak-core:jar:21.0.1:compile
|  +- org.keycloak:keycloak-common:jar:21.0.1:compile
|  +- org.jboss.resteasy:resteasy-client:jar:4.7.7.Final:compile
|  |  +- org.jboss.resteasy:resteasy-client-api:jar:4.7.7.Final:compile
|  |  +- org.jboss.resteasy:resteasy-core-spi:jar:4.7.7.Final:compile
|  |  |  \- org.jboss.spec.javax.annotation:jboss-annotations-api_1.3_spec:jar:2.0.1.Final:compile
|  |  +- org.jboss.resteasy:resteasy-core:jar:4.7.7.Final:compile
|  |  |  +- com.ibm.async:asyncutil:jar:0.1.0:compile
|  |  |  \- io.smallrye.config:smallrye-config:jar:2.3.0:compile
|  |  |     \- io.smallrye.config:smallrye-config-core:jar:2.3.0:compile
|  |  |        +- org.eclipse.microprofile.config:microprofile-config-api:jar:2.0:compile
|  |  |        +- io.smallrye.common:smallrye-common-annotation:jar:1.6.0:compile
|  |  |        +- io.smallrye.common:smallrye-common-expression:jar:1.6.0:compile
|  |  |        |  \- io.smallrye.common:smallrye-common-function:jar:1.6.0:compile
|  |  |        +- io.smallrye.common:smallrye-common-constraint:jar:1.6.0:compile
|  |  |        +- io.smallrye.common:smallrye-common-classloader:jar:1.6.0:compile
|  |  |        \- io.smallrye.config:smallrye-config-common:jar:2.3.0:compile
|  |  +- org.apache.httpcomponents:httpclient:jar:4.5.14:compile
|  |  |  \- org.apache.httpcomponents:httpcore:jar:4.4.16:compile
|  |  +- commons-codec:commons-codec:jar:1.15:compile
|  |  +- commons-io:commons-io:jar:2.9.0:compile
|  |  +- org.jboss.spec.javax.ws.rs:jboss-jaxrs-api_2.1_spec:jar:2.0.1.Final:compile
|  |  \- org.reactivestreams:reactive-streams:jar:1.0.4:compile
|  +- org.jboss.resteasy:resteasy-multipart-provider:jar:4.7.7.Final:compile
|  |  +- com.sun.mail:jakarta.mail:jar:1.6.5:compile
|  |  +- org.apache.james:apache-mime4j-dom:jar:0.8.3:compile
|  |  |  \- org.apache.james:apache-mime4j-core:jar:0.8.3:compile
|  |  \- org.apache.james:apache-mime4j-storage:jar:0.8.3:compile
|  +- org.jboss.resteasy:resteasy-jackson2-provider:jar:4.7.7.Final:compile
|  |  +- com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:jar:2.14.3:compile
|  |  |  +- com.fasterxml.jackson.jaxrs:jackson-jaxrs-base:jar:2.14.3:compile
|  |  |  \- com.fasterxml.jackson.module:jackson-module-jaxb-annotations:jar:2.14.3:compile
|  |  \- com.github.java-json-tools:json-patch:jar:1.13:compile
|  |     +- com.github.java-json-tools:msg-simple:jar:1.2:compile
|  |     |  \- com.github.java-json-tools:btf:jar:1.3:compile
|  |     \- com.github.java-json-tools:jackson-coreutils:jar:2.0:compile
|  \- org.jboss.resteasy:resteasy-jaxb-provider:jar:4.7.7.Final:compile
|     \- org.jboss.spec.javax.xml.bind:jboss-jaxb-api_2.3_spec:jar:2.0.0.Final:compile
+- com.mysql:mysql-connector-j:jar:8.0.33:runtime
+- io.micrometer:micrometer-registry-prometheus:jar:1.10.9:compile
|  \- io.prometheus:simpleclient_common:jar:0.16.0:compile
|     \- io.prometheus:simpleclient:jar:0.16.0:compile
|        +- io.prometheus:simpleclient_tracer_otel:jar:0.16.0:compile
|        |  \- io.prometheus:simpleclient_tracer_common:jar:0.16.0:compile
|        \- io.prometheus:simpleclient_tracer_otel_agent:jar:0.16.0:compile
+- com.github.javafaker:javafaker:jar:1.0.2:compile
|  +- org.apache.commons:commons-lang3:jar:3.12.0:compile
|  +- org.yaml:snakeyaml:jar:android:1.23:compile
|  \- com.github.mifmif:generex:jar:1.0.2:compile
|     \- dk.brics.automaton:automaton:jar:1.11-8:compile
+- org.modelmapper:modelmapper:jar:3.0.0:compile
+- com.fasterxml.jackson.core:jackson-databind:jar:2.14.3:compile
|  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.14.3:compile
|  \- com.fasterxml.jackson.core:jackson-core:jar:2.14.3:compile
+- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.14.3:compile
\- org.springdoc:springdoc-openapi-starter-webmvc-ui:jar:2.2.0:compile
   +- org.springdoc:springdoc-openapi-starter-webmvc-api:jar:2.2.0:compile
   |  \- org.springdoc:springdoc-openapi-starter-common:jar:2.2.0:compile
   |     \- io.swagger.core.v3:swagger-core-jakarta:jar:2.2.15:compile
   |        +- io.swagger.core.v3:swagger-annotations-jakarta:jar:2.2.15:compile
   |        +- io.swagger.core.v3:swagger-models-jakarta:jar:2.2.15:compile
   |        \- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:jar:2.14.3:compile
   \- org.webjars:swagger-ui:jar:5.2.0:compile
StareStarrySky commented 1 year ago

the mvn spring-boot:run has a context in spring-boot-maven-plugin maybe, but java -jar not need. Something maybe can be found out from the context.

bclozel commented 1 year ago

I think this is due to devtools setting properties as explained here: https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.devtools.property-defaults (in this case ˋ server.error.*ˋ).

This behavior is disabled when running in jar mode as documented.

snicoll commented 1 year ago

@robozb you have devtools on the classpath, which configures settings for local devlopment. Devtools is not enabled when you start the app as a production app.

Going forward, please ask questions on StackOverflow as this issue tracker is not meant for this. If you want to report an issue, your dependency tree and a video are not the right information to share with us. Rather a minimal sample that we can use to reproduce the issue.

robozb commented 1 year ago

@robozb you have devtools on the classpath, which configures settings for local devlopment. Devtools is not enabled when you start the app as a production app.

Going forward, please ask questions on StackOverflow as this issue tracker is not meant for this. If you want to report an issue, your dependency tree and a video are not the right information to share with us. Rather a minimal sample that we can use to reproduce the issue.

Dear @snicoll,

Thank you so much for your kind, quick answer!

OK, I understand I'll do so in the future!

Have a nice day!

Bela

robozb commented 1 year ago

I think this is due to devtools setting properties as explained here: https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.devtools.property-defaults (in this case ˋ server.error.*ˋ).

This behavior is disabled when running in jar mode as documented.

Dear @bclozel,

OH, great I'm getting understand! Thank you for your time!

Best regards

Bela

robozb commented 1 year ago

Thank you so much, I could solve the problem with: @ControllerAdvice, @ExceptionHandler and a CustomErrorResponse class:

An example implementation:

import jakarta.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.time.LocalDateTime;
import lombok.Data;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public ResponseEntity<CustomErrorResponse> handleException(Exception ex, HttpServletRequest request) {

        Class clazz = ex.getClass();
        HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;

        if (clazz.isAnnotationPresent(ResponseStatus.class)) {
            ResponseStatus annotation = (ResponseStatus) clazz.getAnnotation(ResponseStatus.class);
            status = annotation.value();
        }

        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        ex.printStackTrace(pw);
        String stackTrace = sw.toString();

        CustomErrorResponse errorResponse = new CustomErrorResponse(
            status.value(),
            status.getReasonPhrase(),
            stackTrace,
            ex.getMessage(),
            request.getRequestURI()
        );
        return new ResponseEntity<>(errorResponse, status);
    }

    @Data
    public static class CustomErrorResponse {

        private LocalDateTime timestamp;
        private int status;
        private String error;
        private String trace;
        private String message;
        private String path;

        public CustomErrorResponse(int status, String error, String trace, String message, String path) {
            this.timestamp = LocalDateTime.now();
            this.status = status;
            this.error = error;
            this.trace = trace;
            this.message = message;
            this.path = path;
        }

    }

}

An Exception example class:

import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@NoArgsConstructor 
@Getter
public class DetailedError extends Exception {

    public DetailedError(String message) {
        super(message);
    }

    public DetailedError(String message, Throwable cause) {
        super(message, cause);
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST) 
    @Getter
    public static class BadRequest extends DetailedError  {

        public BadRequest(String message) {
            super(message);
        }

        public BadRequest(String message, Throwable cause) {
            super(message, cause);
        }

        public BadRequest() {
        }

    }

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) 
    public static class InternalServerError extends DetailedError  {

        public InternalServerError(String message) {
            super(message);
        }

        public InternalServerError(String message, Throwable cause) {
            super(message, cause);
        }

        public InternalServerError() {
        }      

    }    

    @ResponseStatus(HttpStatus.NOT_FOUND) 
    public static class NotFound extends DetailedError  {

        public NotFound(String message) {
            super(message);
        }

        public NotFound(String message, Throwable cause) {
            super(message, cause);
        }

        public NotFound() {
        }

    } 

}
snicoll commented 1 year ago

I don't understand what you're doing. If you want trace, please read the doc I've referenced and set server.error.include-stacktrace=always in your configuration. No need for custom code.

robozb commented 1 year ago

OK, great, thank you! I thought It depends on the existence of the DevTools! Thanks again!