Java Rest控制器未在spring引导中映射

Java Rest控制器未在spring引导中映射,java,spring,rest,spring-boot,Java,Spring,Rest,Spring Boot,我的应用程序正在运行,但控制台中没有显示任何有关映射的内容。我的应用程序类文件位于控制器之上,并且还添加了@ComponentScan(basePackages:“com.org.name.controller”)来扫描控制器。控制台中仍然没有显示映射。我在控制器类中注释掉了@Autowired,因为我得到了以下错误: Error starting ApplicationContext. To display the conditions report re-run your applicati

我的应用程序正在运行,但控制台中没有显示任何有关映射的内容。我的应用程序类文件位于控制器之上,并且还添加了@ComponentScan(basePackages:“com.org.name.controller”)来扫描控制器。控制台中仍然没有显示映射。我在控制器类中注释掉了@Autowired,因为我得到了以下错误:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-07-12 11:05:16.014 ERROR 14104 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

Field userService in com.homzhub.lms.controller.UserController required a bean of type 'com.homzhub.lms.service.UserService' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.homzhub.lms.service.UserService' in your configuration.
主要类别:

package com.homzhub.lms;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages = {"com.homzhub.lms.controller"})
//@EnableJpaRepositories("repository")
//@EnableAutoConfiguration
public class LmsApplication{
    public static void main(String[] args){
        SpringApplication.run(LmsApplication.class, args);
    }
}
预约控制员:

package com.homzhub.lms.controller;

import java.security.Principal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.homzhub.lms.entity.Appointment;
import com.homzhub.lms.entity.User;
import com.homzhub.lms.service.AppointmentService;
import com.homzhub.lms.service.UserService;



@Controller
@RequestMapping(path = "/appointment")
public class AppointmentController {

   // @Autowired
    private AppointmentService appointmentService;

    //@Autowired
    private UserService userService;

    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public void createAppointment(Model model) {
        Appointment appointment = new Appointment();
        model.addAttribute("appointment", appointment);
        model.addAttribute("dateString", "");
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public String createAppointmentPost(@ModelAttribute("appointment") Appointment appointment, @ModelAttribute("dateString") String date, Model model, Principal principal) throws ParseException {

        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        Date d1 = format1.parse(date);
        appointment.setDate(d1);

        User user = userService.findByUsername(principal.getName());
        appointment.setUser(user);

        appointmentService.createAppointment(appointment);

        return "redirect:/userFront";
    }
}
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>1.5.21.RELEASE</version>  -->
        <version>2.1.6.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.homzhub</groupId>
    <artifactId>lms</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>lms</name>
    <description>Lead Management System</description>

    <properties>
        <java.version>1.8</java.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-thymeleaf</artifactId>
        </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>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            </dependency>
    </dependencies>

    <build>
        <plugins>
            <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>
            </plugin>   
        </plugins>
    </build>

</project>
用户服务类:

package com.homzhub.lms.service;

import java.util.List;
import java.util.Set;
import org.springframework.stereotype.Service;
import com.homzhub.lms.entity.User;
import com.homzhub.lms.security.UserRole;
//@Service("userDetailsService")
@Service
public interface UserService{
    User findByUsername(String username);
    User findByEmail(String email);
 //   User findByPhoneNumber(String phoneNumber);
    boolean checkUserExists(String username, String email);
    boolean checkUsernameExists(String username);
    boolean checkEmailExists(String email);
    void save(User user);
    User createUser(User user, Set<UserRole> userRoles);
    User saveUser(User user);
    List<User> findUserList();
    void enableUser(String username);
    void disableUser(String username);
}
包com.homzhub.lms.service;
导入java.util.List;
导入java.util.Set;
导入org.springframework.stereotype.Service;
导入com.homzhub.lms.entity.User;
导入com.homzhub.lms.security.UserRole;
//@服务(“userDetailsService”)
@服务
公共接口用户服务{
用户findByUsername(字符串用户名);
用户findByEmail(字符串电子邮件);
//用户findByPhoneNumber(字符串电话号码);
布尔checkUserExists(字符串用户名、字符串电子邮件);
布尔checkUsernameExists(字符串用户名);
布尔checkEmailExists(字符串电子邮件);
作废保存(用户);
用户createUser(用户用户,设置用户角色);
用户saveUser(用户用户);
列出findUserList();
void enableUser(字符串用户名);
无效禁用用户(字符串用户名);
}

如果尚未定义,则需要将
UserService
定义为组件,或者更恰当地定义为服务。如果它已经存在,您必须映射它,考虑到Spring应该自己做这件事,这有点奇怪。

如果它还没有存在,您需要将
UserService
定义为一个组件,或者更恰当地定义为一个服务。如果它已经存在,你必须映射它,考虑到Spring应该自己做这件事,这有点奇怪。

你只扫描
com.homzhub.lms.controller
,而
用户服务
不在ComponentScan下。 您需要将服务包添加到ComponentScan


@ComponentScan(basePackages={“com.homzhub.lms”})
您只扫描
com.homzhub.lms.controller
,而
UserService
不在ComponentScan下。 您需要将服务包添加到ComponentScan


@ComponentScan(basePackages={“com.homzhub.lms”}
您的服务在
com.homzhub.lms.service
包下,因此您也必须将此包添加到
@ComponentScan
中,因此Spring也将扫描此包,并从中提取标记有原型的类:

@SpringBootApplication
@ComponentScan(basePackages = {"com.homzhub.lms.controller, "com.homzhub.lms.service"})
public class LmsApplication{
    public static void main(String[] args){
        SpringApplication.run(LmsApplication.class, args);
    }
}
但是我可以看到,用
@SpringBootApplication
注释的类已经在所有组件包之上,因此您可以完全摆脱
@ComponentScan
注释。因此,默认情况下,它将扫描嵌套包


还请记住使用Spring原型注释来注释您的服务类,例如
@service
,以便组件扫描能够提取它们。

您的服务位于
com.homzhub.lms.service
包下,因此您也必须将此包添加到
@ComponentScan
,因此Spring也会扫描这个包,并在那里选择标记有原型的类:

@SpringBootApplication
@ComponentScan(basePackages = {"com.homzhub.lms.controller, "com.homzhub.lms.service"})
public class LmsApplication{
    public static void main(String[] args){
        SpringApplication.run(LmsApplication.class, args);
    }
}
但是我可以看到,用
@SpringBootApplication
注释的类已经在所有组件包之上,因此您可以完全摆脱
@ComponentScan
注释。因此,默认情况下,它将扫描嵌套包


还记得如何使用Spring原型注释来注释您的服务类,例如
@service
,以便组件扫描能够拾取它们。

取消注释
@Autowired
注释。将
@Service
注释放在实现类而不是接口上,并确保您的实现类可以通过
componentScan
发现 另外,作为旁注,Spring将扫描主类(带有
@SpringBootApplication
注释的类)的所有子包。因此,如果希望将实现保持在不同的包中,最好将目录结构(如
com.homzhub.lms
作为根目录)和
com.homzhub.lms.controller
作为控制器
com.homzhub.lms.service
作为服务和
com.homzhub.lms.service.impl

如果您遵循此结构,则不需要
组件扫描

取消注释
@Autowired
注释。将
@Service
注释放在实现类而不是接口上,并确保您的实现类可以通过
componentScan
发现 另外,作为旁注,Spring将扫描主类(带有
@SpringBootApplication
注释的类)的所有子包。因此,如果希望将实现保持在不同的包中,最好将目录结构(如
com.homzhub.lms
作为根目录)和
com.homzhub.lms.controller
作为控制器
com.homzhub.lms.service
作为服务和
com.homzhub.lms.service.impl

如果您遵循此结构,则不需要
componentScan

您的服务在
com.homzhub.lms.service
包下,因此您也必须将此包添加到组件扫描中。@michalk在MainApplication类中将com.homzhub.lms.service添加为componentScan,但仍然没有映射。控制台输出与上面相同。您的服务在
com.homzhub.lms.service
包下,因此您也必须将此包添加到组件扫描中。@michalk在MainApplication类中将com.homzhub.lms.service添加为ComponentScan,但仍然没有映射。控制台输出与上面相同。我在上面添加了UserService类,请看一看。我已经在UserService类中添加了@Service注释。我已经在上面添加了UserService类,请看一看。我已经在UserService类中添加了@Service注释。我添加了:@ComponentScan(basePackages={“com.homzhub.lms.controller”})@ComponentScan(basePackages={“com.homzhub.lms.Service”})和我的服务类hav