SpringBoot中的server.context-path

目录

一、问题引入

二、代码片段展示 

2.1.接口层 

2.2.application.properties

三、问题分析 

3.1.server.context-path 作用

3.2.正确展示 

四、HTTP请求响应码简介 

4.1.响应码参考来源

4.2.源码示例 

 4.2.1.源码总述

 4.2.2.正常情况——2XX: generally "OK"

 4.2.3.重定位/重定向——3XX: relocation/redirect

4.2.3.1.300: Multiple Choices. 

4.2.3.2.301: Moved Permanently. 

4.2.3.3.302: Temporary Redirect. 

4.2.3.4.303: See Other. 

4.2.3.5.304: Not Modified. 

4.2.3.6.305: Use Proxy. 

4.2.4.连接错误——4XX: client error

4.2.5.服务错误——5XX: server error


一、问题引入

         书接上回,SpringBoot 在 idea中的 .idea和 .iml文件-CSDN博客我在boot-test的测试项目中使用的 SpringBoot版本为 1.3.5.RELEASE,新项目 cps-task中使用的版本为 2.4.8,造成了连接异常,问题很好解决,但涉及的bug记录一下。

        首先,先看一张图片

二、代码片段展示 

2.1.接口层 

package com.bt.controller;

import com.bt.config.DataConfig;
import com.bt.service.ReconciliationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
@Controller
public class ReconciliationController implements DataConfig {

    @Resource
    private ReconciliationService reconciliationService;


    @RequestMapping(value = "/api/cib", method = RequestMethod.GET)
    @ResponseBody
    public String cibReconciliation(HttpServletRequest request){
        String date = request.getParameter("date");
        if(date == null){
            return "date is null";
        }
        Pattern pattern = Pattern.compile(DATE_PATTERN);
        Matcher matcher = pattern.matcher(date);
        if (!matcher.matches()) {
            // 不输入符合预期的格式,进行下一步操作
            return "The format of 'date' is illegal!";
        }
        log.info("执行对账开始!!");
        reconciliationService.checkBillForReconciliation(BANK_ID_CIB, date);
        return "ok";
    }
}

2.2.application.properties

server.port=8899
server.context-path=/cps-task/

三、问题分析 

3.1.server.context-path 作用

         在Spring Boot项目中,server.context-path 属性用于设置应用程序的上下文路径(context path),这样应用就不会直接部署在根路径(/)下,而是部署在指定的路径下。然而,需要注意的是,从Spring Boot 2.0开始,server.context-path 属性已经被弃用,并被 server.servlet.context-path 所替代。

        我这里在 2.4.8 的高版本中,使用了低版本的参数,造成参数未生效,所以,出现了404连接异常。

3.2.正确展示 

        对于Spring Boot 1.x版本,你可以在你的application.propertiesapplication.yml文件中这样设置:

        application.properties  

server.context-path=/cps-task/


        对于Spring Boot 2.x及更高版本,application.properties 文件应该这样设置:

server.servlet.context-path=/cps-task/

        而application.yml 的YAML格式的配置文件,应该这样设置:

# application.yml  
server:  
  servlet:  
    context-path: /cps-task/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值