• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

spring boot学习第二篇:spring boot2.6.4版本启动接口服务

武飞扬头像
veminhe
帮助1

1、先启动好

需要改动2个地方

pom.xml里面

  1.  
    <parent>
  2.  
    <groupId>org.springframework.boot</groupId>
  3.  
    <artifactId>spring-boot-starter-parent</artifactId>
  4.  
    <version>2.6.4</version>
  5.  
    <relativePath/> <!-- lookup parent from repository -->
  6.  
    </parent>

 启动后,发现使用http://localhost:8080/demo/api

访问不行

访问http://localhost:8080/api

却是好的。 

无意中看到阿里云开发者平台资料

Spring 全家桶之 Spring Boot 2.6.4(二)- Configuration(Part B)(下)-阿里云开发者社区

注意观察Tomcat started on port(s): 8080 (http) with context path '/demo'这样的启动日志

配置工程上下文根的配置,应该是server.servlet.context-path=/demo这样的,改了后,重启服务

 学新通

 访问http://localhost:8080/demo/api

 接口正常

学新通

2、贴上代码

2.1pom.xml文件内容如下:

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.  
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.  
    <modelVersion>4.0.0</modelVersion>
  5.  
     
  6.  
    <groupId>com.hmblogs</groupId>
  7.  
    <artifactId>demo</artifactId>
  8.  
    <version>0.0.1-SNAPSHOT</version>
  9.  
    <packaging>jar</packaging>
  10.  
     
  11.  
    <name>demo</name>
  12.  
    <description>Demo project for Spring Boot</description>
  13.  
     
  14.  
    <parent>
  15.  
    <groupId>org.springframework.boot</groupId>
  16.  
    <artifactId>spring-boot-starter-parent</artifactId>
  17.  
    <version>2.6.4</version>
  18.  
    <relativePath/> <!-- lookup parent from repository -->
  19.  
    </parent>
  20.  
     
  21.  
    <properties>
  22.  
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  23.  
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  24.  
    <java.version>1.8</java.version>
  25.  
    </properties>
  26.  
     
  27.  
    <dependencies>
  28.  
    <dependency>
  29.  
    <groupId>org.springframework.boot</groupId>
  30.  
    <artifactId>spring-boot-starter-web</artifactId>
  31.  
    </dependency>
  32.  
     
  33.  
    <dependency>
  34.  
    <groupId>org.springframework.boot</groupId>
  35.  
    <artifactId>spring-boot-starter-test</artifactId>
  36.  
    <scope>test</scope>
  37.  
    </dependency>
  38.  
    </dependencies>
  39.  
     
  40.  
     
  41.  
     
  42.  
     
  43.  
    </project>
学新通

2.2application.properties文件内容如下:

  1.  
    server.port=8080
  2.  
    server.servlet.context-path=/demo

2.3HelloController.java文件内容如下:

  1.  
    package com.example.demo;
  2.  
     
  3.  
    import org.springframework.web.bind.annotation.RestController;
  4.  
    import org.springframework.web.bind.annotation.RequestMapping;
  5.  
     
  6.  
    @RestController
  7.  
    public class HelloController {
  8.  
     
  9.  
    @RequestMapping("/api")
  10.  
    public String index() {
  11.  
    return "Greetings from Spring Boot!";
  12.  
    }
  13.  
     
  14.  
    }

2.4DemoApplication.java文件内容如下:

  1.  
    package com.example.demo;
  2.  
     
  3.  
    import org.springframework.boot.SpringApplication;
  4.  
    import org.springframework.boot.autoconfigure.SpringBootApplication;
  5.  
     
  6.  
    @SpringBootApplication
  7.  
    public class DemoApplication {
  8.  
     
  9.  
    public static void main(String[] args) {
  10.  
    SpringApplication.run(DemoApplication.class, args);
  11.  
    }
  12.  
     
  13.  
    }

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgjgbce
系列文章
更多 icon
同类精品
更多 icon
继续加载