NullPointerException@在服务上自动连线.JAX-RS+SpringBoot+WildFly



你好,我是SpringBoot的新手,不知道如何解决这个问题。我使用的是ApacheDS服务器,我正在构建一个Rest控制器和usching Spring Security。我从上周就遇到了这个问题,我需要解决它。当我试图从@Autowired服务获得简单的返回时,它总是空的。这是我的configClass。这不是问题。。。一切都很好。直到我用这个路径到达我的控制器";http://127.0.0.1:9080/RestServices-0.0.1-SNAPSHOT/git/rest/autowired/2";。一切都很好,但当我尝试使用自动连接服务的单个方法时,我遇到了一个空指针异常。我正在使用WildFly 14

@Configuration @SpringBootApplication(scanBasePackages= "eu.a2a.ent.restservice.service","eu.a2a.ent.restservice.testautowired"})public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Autowired
private AuthenticationEntryPoint authenticationEntryPoint;  
private static Logger logger = LoggerFactory.getLogger(SecurityConfig.class);

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception{ 

logger.debug("Starting authentication {}",auth.ldapAuthentication().hashCode());

auth            
.ldapAuthentication() /
.userDnPatterns("uid={0},ou=people,dc=example,dc=com")         
.contextSource() 
.url("ldap://localhost:10389/")
.and()
.passwordCompare() 
.passwordEncoder(new LdapShaPasswordEncoder())      
.passwordAttribute("userPassword");             
}

@Override
protected void configure(HttpSecurity http) throws Exception {

http.csrf().disable();
http            
.authorizeRequests() 
.anyRequest() 
.fullyAuthenticated() 
.and()
.httpBasic()            
.authenticationEntryPoint(authenticationEntryPoint); 
}
public static void main(String[] args) throws Exception { 

logger.info("Sono passato per il run");
SpringApplication.run(SecurityConfig.class, "--debug");         
}           
}

这是我的休息控制器

@Path("/rest")public class SampleService {
@Autowired
private IAutowired autowired;
private static Logger logger = LoggerFactory.getLogger(SampleService.class);
@GET
@Path("/hello/{name}")
public String hello(@PathParam("name") String name)
{
return "hi " + name; //this works
}

@GET
@Path("/autowired")
public String autowired()
{
return autowired.hello();   
}   

}

这是我的界面

public interface IAutowired {
String hello();
}

和我的服务

@Service
public class AutowiredService implements IAutowired {
@Override
public String saluta() {
return "Hello, I'm Hello";
}
}

如果你需要,这是我的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 http://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.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<artifactId>RestServices</artifactId>
<groupId>eu.a2a.ent</groupId>
<name>Spring Boot Security Sample</name>
<description>Spring Boot Security Sample</description>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
<springframework.version>4.0.2.RELEASE</springframework.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>       
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>            
<exclusions>
<exclusion>      
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.0.1</version>        
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>        
</dependency>  
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>            
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>            
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>           
</dependency>       
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-ldap</artifactId>           
</dependency>       
<dependency>
<groupId>com.unboundid</groupId>
<artifactId>unboundid-ldapsdk</artifactId>          
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>            
<scope>provided</scope>
</dependency>        

<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>                
</plugin>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>2.0.0.Final</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<excludes>
<exclude>eu/a2a/ent/restservice/secure/SecurityConfig.java</exclude>
</excludes>
</configuration>
</plugin>              
</plugins>
<finalName>ENT</finalName>
</build>    
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>

<!-- Repository per Oracle funzionante-->
<repository>
<id>oracle_repository</id>
<url>http://www.datanucleus.org/downloads/maven2/</url>
</repository>

</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>

我已经尝试过在我的"AutowiredService";也一样,但似乎不起作用。这是一个相当大的问题,因为我总是这样使用@Autowired注释,而且它很有效。我现在不明白为什么很多人像我一样使用这个注释并工作,但它不适合我。

AuthenticationEntryPoint的包结构是什么?您需要确保bean是在以下任一包下定义的

  1. eu.a2a.ent.restservice.service
  2. eu.a2a.ent.restservice.testautowired

最新更新