首页 > 解决方案 > 我应该怎么办?event.ReminderApp.model.Event 中构造函数的参数 0 需要找不到类型为“java.util.UUID”的 bean

问题描述

这是我的课程,我不知道我应该怎么做才能解决这个错误。对不起,如果我只是在这里发帖,但我想让你知道这一切。

我想创建一个后端应用程序,我可以使用 Postgres 和 JDBC 存储有关事件的详细信息。这是整个错误 > 启动 ApplicationContext 时出错。要显示条件报告,请在启用“调试”的情况下重新运行您的应用程序。2020-07-15 15:28:52.428 错误 9748 --- [主要] osbdLoggingFailureAnalysisReporter:


应用程序无法启动


描述:

event.ReminderApp.model.Event 中构造函数的参数 0 需要找不到类型为“java.util.UUID”的 bean。

注入点有以下注解: - @com.fasterxml.jackson.annotation.JsonProperty(index=-1, access=AUTO, value="id", defaultValue="", required=false)

行动:

考虑在您的配置中定义一个“java.util.UUID”类型的 bean。

事件.java

package event.ReminderApp.model;

import com.fasterxml.jackson.annotation.JsonProperty;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.autoconfigure.SpringBootApplication;


import java.util.Date;
import java.util.UUID;

@SpringBootApplication

public class Event {

    private final UUID id;
    private final String name;
    private final Date startDate;
    private final Date endDate;
    private final String details;


    @Autowired
    public Event(@JsonProperty("id") UUID id,
                 @JsonProperty("name") String name,
                 @JsonProperty("startDate") Date startDate,
                 @JsonProperty("endDate") Date endDate,
                 @JsonProperty("details") String details) {

        this.id = id;
        this.name = name;
        this.startDate = startDate;
        this.endDate = endDate;
        this.details = details;
    }

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.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>event</groupId>
    <artifactId>ReminderApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ReminderApp</name>
    <description>This app is a google calendar clone</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>

        <!-- https://mvnrepository.com/artifact/javax.persistence/javax.persistence-api -->
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
        </dependency>

        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>2.1.17.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>jakarta.validation</groupId>
            <artifactId>jakarta.validation-api</artifactId>
            <version>2.0.2</version>
        </dependency>


        <dependency>
            <groupId>com.sun.istack</groupId>
            <artifactId>istack-commons-runtime</artifactId>
            <version>3.0.11</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.2.7.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <!-- Activate the use of TCP to transmit events to the plugin -->
                    <forkNode implementation="org.apache.maven.plugin.surefire.extensions.SurefireForkNodeFactory"/>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>9</source>
                    <target>9</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

事件数据库服务.Java

package event.ReminderApp.dao;

import event.ReminderApp.model.Event;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Repository

public class EventDatabaseService implements EventInterface {

    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public EventDatabaseService(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public int insertEvent(UUID id, Event Event) {
        return 0;
    }

    @Override
    public List<Event> selectAllEvents() {
        final String sql = "SELECT id, eventName, startdate, enddate, details FROM event";
        return jdbcTemplate.query(sql, (resultSet, i )-> {
            UUID eventId =  UUID.fromString(resultSet.getString("id"));
            String eventName = resultSet.getString("eventName");
            Date startDate = resultSet.getDate("startdate");
            Date endDate = resultSet.getDate("endDate");
            String details = resultSet.getString("details");
            return new Event(eventId, eventName, startDate, endDate, details);
        });
    }

    @Override
    public Optional<Event> selectEventById(UUID id) {
        return Optional.empty();
    }

    @Override
    public int updateEventById(UUID id, Event Event) {
        return 0;
    }

    @Override
    public int deleteEventById(UUID id) {
        return 0;
    }
}

事件控制器.java


package event.ReminderApp.api;

import event.ReminderApp.model.Event;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import event.ReminderApp.service.EventService;

import javax.validation.Valid;

import javax.validation.constraints.NotNull;

import java.util.List;

import java.util.UUID;


@RestController
@RequestMapping("event/ReminderApp/api/v1/event")

public class EventController {

    public final EventService eventService;
    
    @Autowired
    public EventController(EventService eventService) {
        this.eventService = eventService;
    }

    @PostMapping
    public void addEvent(@RequestBody Event event)  {
        eventService.addEvent(event);
    }

    @GetMapping
    public List<Event> getAllEvents() {
        return eventService.getAllEvents();
    }

    @GetMapping("{id}")
    public Event getEventById(@PathVariable("id") UUID id) {
        return eventService.getEventById(id)
                .orElse(null);
    }

    @PutMapping("{id}")
    public void updateEvent(@PathVariable("id") UUID id, @Valid @NotNull @RequestBody Event eventToUpdate) {
        eventService.updateEvent(id, eventToUpdate);
    }

    @DeleteMapping("{id}")
    public void deleteEvent(@PathVariable("id") UUID id) {
         eventService.deleteEvent(id);
    }
}

事件服务.java

package event.ReminderApp.service;

import event.ReminderApp.dao.EventInterface;

import event.ReminderApp.model.Event;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.List;

import java.util.Optional;

import java.util.UUID;

@Service
public class EventService {

    public final EventInterface eventInterface;

    @Autowired
    public EventService(EventInterface eventInterface) {
        this.eventInterface = eventInterface;
    }

    public int addEvent(Event event) {
        return eventInterface.insertEvent(event);
    }

    public Optional<Event> getEventById(UUID id) {
        return eventInterface.selectEventById(id);
    }

    public List<Event> getAllEvents() {
        return eventInterface.selectAllEvents();
    }

    public int updateEvent(UUID id, Event event){
        return eventInterface.updateEventById(id, event);
    }

    public int deleteEvent(UUID id) {enter code here
        return eventInterface.deleteEventById(id);
    }
}```

标签: javaspringspring-boot

解决方案


您是否尝试在具有 main 方法的类上使用 @SpringBootApplication ?像这个例子https://spring.io/guides/gs/spring-boot/

package com.example.springboot;

import java.util.Arrays;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return args -> {
            System.out.println("Let's inspect the beans provided by Spring Boot:");
            String[] beanNames = ctx.getBeanDefinitionNames();
            Arrays.sort(beanNames);
            for (String beanName : beanNames) {
                System.out.println(beanName);
            }
        };
    }
}

推荐阅读