新建一个spring boot项目

现在官网上的Quickstart Guide https://spring.io/quickstart

已经都推荐Eclipse,或者idea自动生成代码了

但是还是喜欢敲代码敲出来一个干净的项目

在pom.xml最上面添加

<parent>
        <groupId>cn.dails</groupId>
        <artifactId>dails-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath></relativePath>
    </parent>

这是自己定义的一个parent,用于jar包的版本控制

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

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

在application.yml中添加

server:
  port: 28081
spring:
  application:
    name: dms-user

新建启动类

package cn.dms;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class UserApplication {

    public static void main(String[] args) {
        SpringApplication.run( UserApplication.class, args );
    }

    @Value("${server.port}")
    String port;

    @RequestMapping("/hi")
    public String home(@RequestParam(value = "name", defaultValue = "屌丝") String name) {
        return "hi " + name + " ,i am from port:" + port;
    }
}

好了,正常启动吧
访问http://127.0.0.1:28081/hi

  • 可以在yml里面设置server.base-uri-code=l***
    在路径上配置
    @RequestMapping(“/${server.base-uri-code}/private/bonus”)
    这样达到统一路径

  • 还有个习惯:public/private 塞到路径上
    public不用鉴权
    private 需要鉴权

server.servlet.context-path定义上下文路径

server.servlet.context-path不配置时,默认为 / ,如:localhost:8080/xxxxxx
当server.servlet.context-path有配置时,比如 /api,此时的访问方式为localhost:8080/api/xxxxxx

  • 历史区别、springboot 2.0变革后的配置区别
    1、springboot 2.0之前,配置为 server.context-path
    2、springboot 2.0之后,配置为 server.servlet.context-path
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值