tomcat Spring Boot “无法自动接线”

wkftcu5l  于 2022-11-13  发布在  Spring
关注(0)|答案(1)|浏览(151)

Tomcat总是崩溃并显示错误“无法自动连接...”顺便说一句,我没有使用ArrayList模拟的数据库
我的用户界面:

public interface Users{

    Optional<User> findbyId(int id);

    void create(User user);

    void changesomething(User user);
}

用户类,具有ID、电子邮件、nutzername和密码:

@Data
public class User{

    int id;

    String email;

    String nutzername;

    String password;
    
}

带有服务注解的我的服务类:

@Service
public class UserService implements Users{

    private final ArrayList<User> userlist;

    public UserService(ArrayList<User> userlist) {
        this.userlist = userlist;
    }

    @Override
    public Optional<User> findbyId(int id) {
        if(userlist.get(id) != null){
            return Optional.of(userlist.get(id));
        }else {
            return Optional.empty();
        }

    }

    @Override
    public void create(User user) {
        userlist.add(user);
    }

    @Override
    public void changesomething(User user) {
        int id = user.getId();
        findbyId(id).get().setPassword(user.getPassword());
        findbyId(id).get().setEmail(user.getEmail());
        findbyId(id).get().setPassword(user.getPassword());
    }
}

和Rest控制器来处理接收到的JSON:

@RestController
@RequestMapping("/api/users")
public class UserController {

    private Logger logger = LogManager.getLogger();
    private final Users users;

    public UserController(Users users) {
        this.users = users;
    }

    @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity create(@RequestBody UserRepresentation userRepresentation){
        try {
            users.create(userRepresentation.toUser());
            logger.info("User with id" + userRepresentation.getId() + " created!");
            return ResponseEntity.ok().build();
        }catch (Exception e){
            logger.error(e.getMessage());
            return ResponseEntity.internalServerError().build();
        }
    }

    @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<UserRepresentation> findbyID(@RequestParam("id")int id){
        try {
            if(users.findbyId(id).isPresent()){
                logger.info("User with Id: " + id + " is Present");
                return ResponseEntity.ok(UserRepresentation.from(users.findbyId(id).get()));
            } else {
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e){
            logger.error(e.getMessage());
            return ResponseEntity.internalServerError().build();
        }
    }

    @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity updateUser(@RequestBody UserRepresentation userRepresentation){
        try {
            if(users.findbyId(userRepresentation.getId()).isPresent()){
                logger.info("User with Id: " + userRepresentation.getId() + " is Present");
                users.changesomething(userRepresentation.toUser());
                logger.info("User with Id: " + userRepresentation.getId() + " updated");
                return ResponseEntity.ok().build();
            } else {
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e){
            logger.error(e.getMessage());
            return ResponseEntity.internalServerError().build();
        }
    }
}

此处显示错误:

2022-07-30 11:38:36.455  INFO 22648 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1888 ms
2022-07-30 11:38:36.531  WARN 22648 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancellin
g refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController' defined in URL [jar:file:/C:/Use
rs/chris/Desktop/SWT-Programmieren/Users/rest-api/target/rest-api-1.0-SNAPSHOT.jar!/de/christoph/UserController.class]: Unsatisfied dependency expressed through con
structor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'de.christoph.Users' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2022-07-30 11:38:36.538  INFO 22648 --- [           main] o.a.c.c.StandardService                  : Stopping service [Tomcat]
2022-07-30 11:38:36.569  INFO 22648 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-07-30 11:38:36.611 ERROR 22648 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in de.christoph.UserController required a bean of type 'de.christoph.Users' that could not be found.

Action:

Consider defining a bean of type 'de.christoph.Users' in your configuration.

如果你需要更多的信息,只要发短信给我,谢谢帮助:)

7uhlpewt

7uhlpewt1#

您需要将@Autowired添加到UserController中的Users类。

public class UserController {

    private Logger logger = LogManager.getLogger();

@Autowired
    private final Users users;

相关问题