springboot with hibernate Internal Server Error but status code 200

ぐ巨炮叔叔 提交于 2020-01-17 15:16:22

问题


I'm quite new to Spring and I have created a small webservice with Spring-boot, Hibernate and Swagger. Here is my HomeController:

package io.swagger.configuration;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Home redirection to swagger api documentation
 */
@Controller
public class HomeController {
    @RequestMapping(value = "/")
    public String index() {
        System.out.println("swagger-ui.html");
        return "redirect:swagger-ui.html";
    }
}

Everything is working well, excepting I don't understand why when I have an Internal Server Error I get a status 200 with this body for example:

{
    "timestamp": "2017-12-12T23:52:02.306+0000",
    "status": 500,
    "error": "Internal Server Error",
    "exception": "javax.persistence.PersistenceException",
    "message": "org.hibernate.exception.ConstraintViolationException: could not execute statement",
    "path": "/example/members/1031/subscriptions"
}

And there is the launcher :

package io.swagger;

import org.apache.commons.logging.LogFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;

import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2
@ComponentScan(basePackages = "io.swagger")
public class Swagger2SpringBoot extends SpringBootServletInitializer implements CommandLineRunner {

    @Override
    public void run(String... arg0) throws Exception {
        if (arg0.length > 0 && arg0[0].equals("exitcode")) {
            throw new ExitException();
        }
    }

    public static void main(String[] args) throws Exception {

        LogFactory.getLog(Swagger2SpringBoot.class).warn("test");

        new SpringApplication(Swagger2SpringBoot.class).run(args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Swagger2SpringBoot.class);
    }

    class ExitException extends RuntimeException implements ExitCodeGenerator {
        private static final long serialVersionUID = 1L;

        @Override
        public int getExitCode() {
            return 10;
        }

    }
}

with the configuration :

package io.swagger;

import java.util.List;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

@Configuration
public class WebConfiguration extends WebMvcConfigurerAdapter {

      /**
       * Make sure dates are serialised in
       * ISO-8601 format instead as timestamps
       */
      @Override
      public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
          for (HttpMessageConverter<?> converter : converters) {
              if (converter instanceof MappingJackson2HttpMessageConverter) {
                  MappingJackson2HttpMessageConverter jsonMessageConverter  = (MappingJackson2HttpMessageConverter) converter;
                  ObjectMapper objectMapper  = jsonMessageConverter.getObjectMapper();
                  objectMapper.disable(
                    SerializationFeature.WRITE_DATES_AS_TIMESTAMPS
                  );
                  break;
              }
          }
      }
}

Another one for Swagger:

package io.swagger.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
public class SwaggerDocumentationConfig {

    ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("**** API").description("No description").license("").licenseUrl("")
                .termsOfServiceUrl("www.****.com").version("1.0.0")
                .contact(new Contact("", "", "****@****.com")).build();
    }

    @Bean
    public Docket customImplementation() {
        return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors.basePackage("io.swagger.api")).build().apiInfo(apiInfo());
    }

}

So why do I have this 200 status if I got a server error? At least I'd like to generate a status 500, 200 means everything was fine, and obviously it's not. Any idea ?


回答1:


It appears I had implemented a custom error controller generating code from my swagger file, I deleted it and now everything is ok.



来源:https://stackoverflow.com/questions/47783858/springboot-with-hibernate-internal-server-error-but-status-code-200

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!