带有休眠内部服务器错误但状态代码为200的springboot [英] springboot with hibernate Internal Server Error but status code 200

查看:124
本文介绍了带有休眠内部服务器错误但状态代码为200的springboot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Spring还是很陌生,并且已经用Spring-bootHibernateSwagger创建了一个小型Web服务.这是我的HomeController:

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";
    }
}

一切正常,除了我不明白为什么当我遇到内部服务器错误时,例如,我在此正文中得到一个status 200:

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"
}

有启动器:

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;
        }

    }
}

具有以下配置:

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;
              }
          }
      }
}

Swagger的另一个:

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());
    }

}

那么,如果发生服务器错误,为什么我会显示200状态?至少我想生成一个status 500,200表示一切都很好,显然不是.有什么主意吗?

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 ?

推荐答案

看来我已经实现了一个自定义错误控制器,该错误控制器从swagger文件生成代码,我删除了它,现在一切正常.

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

这篇关于带有休眠内部服务器错误但状态代码为200的springboot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆