我的春季应用未收到我的GET

时间:2018-07-10 18:00:22

标签: java mysql spring

因此,我一直在尝试从spring的官方指南中获取有关将Spring与MySQL(https://spring.io/guides/gs/accessing-data-mysql/)连接的示例

由于我在整个项目中都使用STS,所以我没有100%遵循它。问题是当我使用POSTMAN或仅使用浏览器发送数据时 如 :     localhost:8000/demo/add?name=First&email=someemail@someemailprovider.com

我找不到错误。

java代码与指南中的相同。我的application.properties是这样的:

server.port=8000
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword

我的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>

<groupId>com.example</groupId>
<artifactId>dbDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>dbDemo</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

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

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

我的浏览器给出的答案是: Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Tue Jul 10 21:15:52 EEST 2018 There was an unexpected error (type=Not Found, status=404). No message available

还有邮递员:

{
"timestamp": "2018-07-10T18:08:28.208+0000",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/demo/add"
}

spring控制台的日志是:

 :: Spring Boot ::        (v2.0.3.RELEASE)

2018-07-10 20:32:36.453  INFO 7815 --- [           main] com.example.demo.DbDemoApplication       : Starting DbDemoApplication on max-kans with PID 7815 (/home/max/Desktop/Fetina/Earino/projectTL/softEngine/dbDemo/target/classes started by max in /home/max/Desktop/Fetina/Earino/projectTL/softEngine/dbDemo)
2018-07-10 20:32:36.496  INFO 7815 --- [           main] com.example.demo.DbDemoApplication       : No active profile set, falling back to default profiles: default
2018-07-10 20:32:36.742  INFO 7815 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2aa5fe93: startup date [Tue Jul 10 20:32:36 EEST 2018]; root of context hierarchy
2018-07-10 20:32:41.650  INFO 7815 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$5ab04215] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-07-10 20:32:42.775  INFO 7815 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8000 (http)
2018-07-10 20:32:43.344  INFO 7815 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-07-10 20:32:43.344  INFO 7815 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.31
2018-07-10 20:32:44.326  INFO 7815 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib]
2018-07-10 20:32:47.580  INFO 7815 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-07-10 20:32:47.581  INFO 7815 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 10868 ms
2018-07-10 20:32:48.187  INFO 7815 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-07-10 20:32:48.192  INFO 7815 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-07-10 20:32:48.193  INFO 7815 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-07-10 20:32:48.193  INFO 7815 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-07-10 20:32:48.193  INFO 7815 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-07-10 20:32:50.090  INFO 7815 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2018-07-10 20:32:54.757  INFO 7815 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2018-07-10 20:32:55.307  INFO 7815 --- [           main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2018-07-10 20:32:55.640  INFO 7815 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
2018-07-10 20:32:58.848  INFO 7815 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.2.17.Final}
2018-07-10 20:32:59.125  INFO 7815 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2018-07-10 20:33:00.513  INFO 7815 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2018-07-10 20:33:02.175  INFO 7815 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2018-07-10 20:33:03.329  INFO 7815 --- [           main] o.h.t.schema.internal.SchemaCreatorImpl  : HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@7a8406c2'
2018-07-10 20:33:03.331  INFO 7815 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2018-07-10 20:33:06.623  INFO 7815 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-07-10 20:33:08.065  INFO 7815 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2aa5fe93: startup date [Tue Jul 10 20:32:36 EEST 2018]; root of context hierarchy
2018-07-10 20:33:08.206  WARN 7815 --- [           main] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2018-07-10 20:33:08.322  INFO 7815 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-07-10 20:33:08.324  INFO 7815 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-07-10 20:33:08.368  INFO 7815 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-07-10 20:33:08.368  INFO 7815 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-07-10 20:33:09.610  INFO 7815 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-07-10 20:33:09.613  INFO 7815 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Bean with name 'dataSource' has been autodetected for JMX exposure
2018-07-10 20:33:09.622  INFO 7815 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Located MBean 'dataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource]
2018-07-10 20:33:10.003  INFO 7815 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8000 (http) with context path ''
2018-07-10 20:33:10.154  INFO 7815 --- [           main] com.example.demo.DbDemoApplication       : Started DbDemoApplication in 35.885 seconds (JVM running for 39.933)
2018-07-10 20:34:07.354  INFO 7815 --- [nio-8000-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-07-10 20:34:07.354  INFO 7815 --- [nio-8000-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2018-07-10 20:34:07.962  INFO 7815 --- [nio-8000-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 608 ms

我的控制器与spring指南中的控制器几乎相同:

package hello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import hello.User;
import hello.UserRepository;

@Controller // This means that this class is a Controller
@RequestMapping(path="/demo")
// This means URL's start with /demo (after Application path)
public class UserController {

    @Autowired
    // This means to get the bean called userRepository
    // Which is auto-generated by Spring, we will use it to handle the data
    private UserRepository userRepository;

    @GetMapping(path="/add") // Map ONLY GET Requests
    public @ResponseBody String addNewUser(@RequestParam String name,
            @RequestParam String email) {
        // @ResponseBody means the returned String is the response,
        // not a view name
        User n = new User();
        n.setName(name);
        n.setEmail(email);
        userRepository.save(n);
        return "Saved";
    }

    @GetMapping(path="/all")
    public @ResponseBody Iterable<User> getAllUsers() {
        // This returns a JSON or XML with the users
        return userRepository.findAll();
    }
}

1 个答案:

答案 0 :(得分:1)

您的@SpringBootApplication类在软件包com.example.demo中(根据启动日志),但是您的@Controller在软件包hello中。默认情况下,Spring Boot不会自动发现@SpringBootApplication类所在包外部的Spring类。将UserController移动到com.example.demo下的某个位置,或者添加{{1 }}注解,将您的@ComponentScan包定位到主Spring Boot应用程序类。