Springboot:无法访问URL

时间:2018-09-27 19:22:53

标签: java spring spring-mvc spring-boot

运行Main类后,我无法获得输出。 Spring Boot的新手

控制器

@Controller 
public class WelcomeController {
    private static final String welcomemsg = "Welcome Mr. %s!";

    @GetMapping("/welcome/user")
    @ResponseBody
    public Welcome welcomeUser(@RequestParam(name = "name", required = false, defaultValue = "Java Fan") String name)
    {
        return new Welcome(String.format(welcomemsg, name));
    } 
}

主班

package com.beans.mainsrc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = { "com.beans" })
public class DemoApplication {
    public static void main(String[] args)
    {
        SpringApplication.run(DemoApplication.class, args);
    }
}

有关运行主类的控制台的详细信息

2018-09-28 00:40:01.268  INFO 2304 --- [           main] com.beans.mainsrc.DemoApplication        : Starting DemoApplication on DESKTOP-551C51M with PID 2304 (F:\springbootdemo\demo\target\classes started by sparsh in F:\springbootdemo\demo) 
2018-09-28 00:40:01.273  INFO 2304 --- [           main] com.beans.mainsrc.DemoApplication        : No active profile set, falling back to default profiles: default
2018-09-28 00:40:01.346  INFO 2304 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3e08ff24: startup date [Fri Sep 28 00:40:01 IST 2018]; root of context hierarchy WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils$1 (file:/C:/Users/sparsh/.m2/repository/org/springframework/spring-core/5.0.9.RELEASE/spring-core-5.0.9.RELEASE.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) WARNING: Please consider reporting this to the maintainers of org.springframework.cglib.core.ReflectUtils$1 WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release 
2018-09-28 00:40:02.370  INFO 2304 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup 
2018-09-28 00:40:02.392  INFO 2304 --- [           main] com.beans.mainsrc.DemoApplication        : Started DemoApplication in 1.67 seconds (JVM running for 2.23) 
2018-09-28 00:40:02.396  INFO 2304 --- [       Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@3e08ff24: startup date [Fri Sep 28 00:40:01 IST 2018]; root of context hierarchy
2018-09-28 00:40:02.398  INFO 2304 --- [       Thread-1] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

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>springboot</groupId>
   <artifactId>springbootdemo</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>
   <name>demo</name>
   <description>Demo project for Spring Boot</description>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.5.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</groupId>
         <artifactId>spring-web</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter</artifactId>
      </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>
</project>

1 个答案:

答案 0 :(得分:3)

好像您的servlet容器没有启动。请尝试添加此Maven依赖项

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

您可以删除这些依赖项:

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

它们由建议的入门者提供。