복붙노트

[SPRING] NoSuchMethodException : org.springframework.boot.autoconfigure.http.HttpMessageConverters

SPRING

NoSuchMethodException : org.springframework.boot.autoconfigure.http.HttpMessageConverters

Hibernate로 Spring 애플리케이션을 설정하고 싶다. 나는 이것을 시도 :

주요 시작 방법 :

@Configuration
@EnableWebMvc
public class WebConfig implements WebApplicationInitializer, WebMvcConfigurer {

    private BasicAuthenticationInterceptor basicAuthenticationInterceptor;

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.removeIf(converter -> converter instanceof MappingJackson2XmlHttpMessageConverter);
        converters.removeIf(converter -> converter instanceof MappingJackson2HttpMessageConverter);
        converters.add(new MappingJackson2XmlHttpMessageConverter(
                ((XmlMapper) createObjectMapper(Jackson2ObjectMapperBuilder.xml()))
                        .enable(ToXmlGenerator.Feature.WRITE_XML_DECLARATION)));
        converters.add(new MappingJackson2HttpMessageConverter(
                createObjectMapper(Jackson2ObjectMapperBuilder.json())));
    }

    private ObjectMapper createObjectMapper(Jackson2ObjectMapperBuilder builder) {
        builder.indentOutput(true);
        builder.modules(new JaxbAnnotationModule());
        builder.serializationInclusion(JsonInclude.Include.NON_NULL);
        builder.defaultUseWrapper(false);
        return builder.build();
    }

    @Override
    public void onStartup(ServletContext container) {
        // Create the 'root' Spring application context
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(ContextDatasource.class);

        // Manage the lifecycle of the root application context
        container.addListener(new ContextLoaderListener(rootContext));

        // Create the dispatcher servlet's Spring application context
        AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();

        // Register and map the dispatcher servlet
        ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }

    @Autowired
    public void setBasicAuthenticationInterceptor(BasicAuthenticationInterceptor basicAuthenticationInterceptor) {
        this.basicAuthenticationInterceptor = basicAuthenticationInterceptor;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(basicAuthenticationInterceptor);
    }

}

rootContext.register (ContextDatasource.class);에서 호출 된 최대 절전 모드 구성;

@SpringBootApplication
@Configuration
@EnableTransactionManagement
public class ContextDatasource {

    @Bean
    public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
        return new FastJsonHttpMessageConverter();
    }

    @Bean
    @Autowired
    public HttpMessageConverters convertersToBeUsed(FastJsonHttpMessageConverter converter) {
        return new HttpMessageConverters(converter);
    }

    @Bean
    public LocalSessionFactoryBean getSessionFactory() throws NamingException {
        final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(getDataSource());
        sessionFactory.setPackagesToScan(new String[] { "org.datalis.plugin.database.models" });
        sessionFactory.setHibernateProperties(hibernateProperties());

        return sessionFactory;
    }

    @Bean
    public DataSource getDataSource() throws NamingException {
        return (DataSource) new JndiTemplate().lookup("java:/global/production_gateway");
    }

    @Bean
    public PlatformTransactionManager getHibernateTransactionManager() throws NamingException {
        final HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(getSessionFactory().getObject());
        return transactionManager;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor getExceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

    private final Properties hibernateProperties() {
        final Properties hibernateProperties = new Properties();
        hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
        hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MariaDBDialect");

        hibernateProperties.setProperty("hibernate.show_sql", "true");
        hibernateProperties.setProperty("hibernate.format_sql", "true");
        return hibernateProperties;
    }
}

하지만 WAR 파일을 배포 할 때 오류가 발생합니다.

Caused by: java.lang.NoSuchMethodException: org.springframework.boot.autoconfigure.http.HttpMessageConverters$$EnhancerBySpringCGLIB$$1d90bff9.<init>()
    at java.base/java.lang.Class.getConstructor0(Class.java:3302)

전체 오류 스택 : https://pastebin.com/x30W2aws

내가 잘못한 곳에서 조언하고 문제를 해결하는 방법을 알려줄 수 있습니까? 다른 구성으로 모듈 시작을 구현해야합니까?

편집하다: Java 8에서는 위의 문제없이 코드가 작동합니다. 최신 Java 10에서는 위의 예외가 발생합니다. 내가해야 할 일을 알고 있니?

해결법

  1. ==============================

    1.Spring Boot 릴리즈 노트에 따르면 Java 10은 Spring Boot 버전 2.0.1 이상에서 지원됩니다. 의존성 목록이 없으면 이것이 문제인지를 아는 것은 불가능하지만 시작할 수있는 좋은 곳처럼 보입니다.

    Spring Boot 릴리즈 노트에 따르면 Java 10은 Spring Boot 버전 2.0.1 이상에서 지원됩니다. 의존성 목록이 없으면 이것이 문제인지를 아는 것은 불가능하지만 시작할 수있는 좋은 곳처럼 보입니다.

    Boot v2.0.1 이상을 실행 중입니까?

  2. from https://stackoverflow.com/questions/51242707/nosuchmethodexception-org-springframework-boot-autoconfigure-http-httpmessageco by cc-by-sa and MIT license