gpt4 book ai didi

java - Spring 启动 : Not able to hit the url

转载 作者:行者123 更新时间:2023-11-29 04:15:38 25 4
gpt4 key购买 nike

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

Controller

@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>

最佳答案

看起来您的 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>

它们由推荐的启动器提供。

关于java - Spring 启动 : Not able to hit the url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52543816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com