在我将HATEOAS的依赖项添加到Maven之后,Spring Boot无法启动:
添加的依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
完整pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>ru.example</groupId>
<artifactId>testapp</artifactId>
<version>1.0</version>
<name>testapp</nAfter I added dependency for HATEOAS to Maven, Spring Boot does not startame>
<description>Test</description>
<properties>
<java.version>1.8</java.version>
<h2.version>1.4.200</h2.version>
<jackson-json.version>2.10.2</jackson-json.version>
<jsoup.version>1.12.1</jsoup.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.17.Final</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>${jsoup.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<version>5.3.0.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-json.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate5</artifactId>
<version>${jackson-json.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson-json.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
SecurityConfig.class:
package ru.example.testapp;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import ru.example.testapp.dao.UserRepository;
import ru.example.testapp.service.UserServiceImpl;
@Configuration
@RequiredArgsConstructor
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private final UserRepository userRepository;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(new UserServiceImpl(userRepository))
.passwordEncoder(new BCryptPasswordEncoder());
}
protected void configure(final HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/rest/admin/**").hasRole("ADMIN").and().httpBasic().and()
.authorizeRequests()
.antMatchers("/rest/user/**").hasAnyRole("USER","ADMIN").and().httpBasic().and()
.authorizeRequests().and()
.csrf().ignoringAntMatchers("/rest/**");
}
}
JacksonObjectMapper.class:
package ru.example.testapp.util.json;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.stereotype.Component;
@Component
public class JacksonObjectMapper extends ObjectMapper {
private static final ObjectMapper MAPPER = new JacksonObjectMapper();
public static ObjectMapper getMapper() {
return MAPPER;
}
private JacksonObjectMapper() {
registerModule(new Hibernate5Module());
registerModule(new JavaTimeModule());
configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
}
在控制台中,我出现以下错误:
org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“ securityConfig”的bean时出错:不满意的依赖关系通过方法'setContentNegotationStrategy'参数0表示;嵌套异常为org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为bean的错误'org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration $ EnableWebMvcConfiguration':通过方法“ setConfigurers”表达的不满意的依赖性参数0;嵌套异常为org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“ hypermediaWebMvcConfigurer”的bean时出错类路径资源[org / springframework / hateoas / config / WebMvcHateoasConfiguration.class]:通过方法表达的不满意依赖性'hypermediaWebMvcConfigurer'参数0;嵌套异常为org.springframework.beans.factory.BeanCreationException:错误创建在类中定义的名称为“ hypermediaWebMvcConverters”的bean路径资源[org / springframework / hateoas / config / HateoasConfiguration.class]:Bean通过工厂方法实例化失败;嵌套异常为org.springframework.beans.BeanInstantiationException:失败实例化[org.springframework.hateoas.config.WebConverters]:工厂方法“ hypermediaWebMvcConverters”抛出异常;嵌套的异常是java.lang.IllegalStateException:失败的copy():ru.example.wmanage.util.json.JacksonObjectMapper(版本:2.10.2)不覆盖copy();它必须
可能是什么问题?错误-具有securityConfig,JacksonObjectMapper和hateoas的东西。如果我删除依赖项下的spring-boot-starter-hateoas,则一切正常。但是我需要仇恨。请帮助。
更新:使用带有批注@Component的自定义JacksonObjectMapper时,会发生此问题。一旦spring-boot-starter-hateoas添加到依赖项,Spting Boot就不会启动。
问题:如何同时使用自定义的JacksonObjectMapper和Hateoas?
未解决的问题
我认为您没有任何理由扩展ObjectMapper。您应该像平常一样实例化ObjectMapper,然后通过其公开的方法对其进行配置,并将其注册为配置中的bean。