一、问题引入
springboot 在 idea中的 .idea和 .iml文件,我在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.properties
或application.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/
四、http请求响应码简介
4.1.响应码参考来源
java.net.httpurlconnection;
4.2.源码示例
4.2.1.源码总述
http的响应代码,从1.1版本开始。
/** * the response codes for http, as of version 1.1. */ // remind: do we want all these?? // others not here that we do want??
4.2.2.正常情况——2xx: generally "ok"
/* 2xx: generally "ok" */ /** * http status-code 200: ok. */ public static final int http_ok = 200; /** * http status-code 201: created. */ public static final int http_created = 201; /** * http status-code 202: accepted. */ public static final int http_accepted = 202; /** * http status-code 203: non-authoritative information. */ public static final int http_not_authoritative = 203; /** * http status-code 204: no content. */ public static final int http_no_content = 204; /** * http status-code 205: reset content. */ public static final int http_reset = 205; /** * http status-code 206: partial content. */ public static final int http_partial = 206;
4.2.3.重定位/重定向——3xx: relocation/redirect
4.2.3.1.300: multiple choices.
multiple choices指当服务器接收到一个请求,并且该请求可以通过多种不同的方式或资源来满足时,服务器可能会返回300 multiple choices状态码。这意味着服务器提供了多个可能的响应,但由客户端来决定选择哪一个。
当客户端收到300 multiple choices状态码时,它应该显示一个包含所有可能响应选项的用户界面,让用户选择一个。然而,由于这个过程可能比较复杂且用户体验不佳,因此很少有网站会这样做。
/** * http status-code 300: multiple choices. */ public static final int http_mult_choice = 300;
4.2.3.2.301: moved permanently.
moved permanently,是一个http响应状态码,表示请求的资源已经被永久地移动到了由location头部所指示的url上。这是一个重定向状态码,它告诉客户端(如浏览器)自动地访问新的url。
特点:
- 永久性:301状态码表示的是永久性的重定向,意味着资源已经不再存在于请求的url上,而是永久地迁移到了新的位置。
- 搜索引擎优化(seo):对于搜索引擎优化(seo)来说,301重定向是一个重要的工具。当网站的结构发生变化,或者网页的url发生更改时,使用301重定向可以帮助搜索引擎将旧url的排名和权重转移到新url上。
- 缓存:由于这是一个永久性的重定向,客户端(如浏览器)和中间缓存(如cdn)可能会缓存这个重定向信息,以便将来对相同资源的请求能够直接访问新的url。
使用场景:
- 当网站的url结构发生变化,但内容仍然需要被访问时。
- 当网站迁移到新的域名或子域名时。
- 当需要将网站的某个部分或页面重命名或重新组织时。
/** * http status-code 301: moved permanently. */ public static final int http_moved_perm = 301;
4.2.3.3.302: temporary redirect.
temporary redirect,是一种http重定向状态码,用于指示客户端发送的请求需要进一步重定向到另一个url,但这次重定向是临时的。与301 moved permanently(永久重定向)不同,307 temporary redirect表明资源的移动是暂时的,客户端在将来可能会再次尝试访问原始url。
特点:
- 保持请求方法:与其他重定向状态码(如302 found)不同,307 temporary redirect要求客户端在重定向时保持原始请求的方法(如get、post等)不变。这意味着,如果原始请求是post请求,重定向后的请求也应该是post请求,而不是自动转换为get请求。
- 临时性:307状态码表明资源的重定向是临时的,可能由于网站维护、服务器负载平衡或url结构临时更改等原因导致。客户端应该继续尝试访问原始url,直到服务器通知其更改已变为永久。
使用场景:
- 网站维护:当网站正在进行临时维护时,服务器可能会将用户的请求重定向到一个备份服务器或维护页面,以避免服务中断。
- url结构更改:在网站重构或升级过程中,可能需要临时更改url结构。使用307 temporary redirect可以确保在更改过程中,用户仍然能够访问网站资源。
- 负载均衡:在分布式系统中,服务器可能会根据当前负载情况将请求重定向到不同的服务器。使用307 temporary redirect可以确保这种重定向是临时的,并且请求方法保持不变。
/** * http status-code 302: temporary redirect. */ public static final int http_moved_temp = 302;
4.2.3.4.303: see other.
see other,是一个http重定向状态码,用于指示客户端使用get方法向另一个uri(统一资源标识符)发送请求,以获取请求的资源。以下是关于303 see other的详细解释:
- 定义:303 see other状态码表示客户端应使用get方法定向获取请求的资源。这通常发生在客户端最初使用post方法发送请求,但服务器希望客户端通过get方法访问资源的另一个uri时。
- 特点:
- 重定向:与301 moved permanently和302 found类似,303 see other也是一种重定向状态码,但它明确要求客户端使用get方法访问新的uri。
- 方法改变:与307 temporary redirect不同,303 see other允许客户端在重定向时改变请求方法,从post(或其他方法)变为get。
使用场景:
- 当客户端使用post方法提交表单,但服务器希望将用户重定向到一个新的页面以显示结果或进行进一步操作时,可以使用303 see other状态码。
- 在某些web应用程序中,为了防止表单的重复提交,服务器可能会在处理完post请求后,使用303 see other将客户端重定向到一个新的get请求页面。
/** * http status-code 303: see other. */ public static final int http_see_other = 303;
4.2.3.5.304: not modified.
not modified,是一个http响应状态码,用于指示客户端请求的资源自上次请求后没有发生改变,因此客户端可以继续使用其缓存的版本,而无需重新下载资源。以下是关于304 not modified的详细解释:
定义与特点:
定义:304 not modified状态码表示客户端请求的资源自上次请求以来没有发生修改,客户端可以使用其缓存的版本。
特点:
- 节省带宽:通过避免不必要的资源下载,可以显著减少网络带宽的使用。
- 提升性能:减少服务器负载,加快页面加载速度,提升用户体验。
- 依赖缓存验证:该状态码通常与缓存验证机制(如etag或last-modified头部)一起使用,以确保资源的最新性。
使用场景:
- 当客户端(如浏览器)已经缓存了某个资源,并且该资源在服务器上未发生更改时,服务器会返回304 not modified状态码。
- 客户端在发送请求时,通常会包含一些条件请求头部(如if-modified-since或if-none-match),以告诉服务器它已经拥有的资源版本信息。
- 服务器在收到这些条件请求头部后,会检查请求的资源是否自上次请求以来有所更改。如果没有更改,服务器就会返回304状态码,并且不会发送资源的内容。
/** * http status-code 304: not modified. */ public static final int http_not_modified = 304;
4.2.3.6.305: use proxy.
use proxy(使用代理)是一个常见的配置或行为,它涉及到通过代理服务器来转发http请求和响应。代理服务器的作用
- 提高访问速度:代理服务器可以缓存客户请求的数据。当多个客户请求相同的数据时,代理服务器可以直接从缓存中提供数据,而无需再次从原始服务器获取,从而提高了访问速度。
- 防火墙和安全:代理服务器可以作为防火墙,限制或过滤用户对某些网站的访问。这有助于保护网络免受恶意软件的攻击,并提高用户的安全性。
- 访问限制资源:在某些情况下,用户可能无法直接访问某些网站或服务。通过代理服务器,用户可以绕过这些限制,访问被限制的资源。
- 隐藏用户身份:使用代理服务器时,外部服务器只能看到代理服务器的ip地址,而无法看到用户的真实ip地址,从而增强了用户的匿名性。
/** * http status-code 305: use proxy. */ public static final int http_use_proxy = 305;
4.2.4.连接错误——4xx: client error
/** * http status-code 400: bad request. */ public static final int http_bad_request = 400; /** * http status-code 401: unauthorized. */ public static final int http_unauthorized = 401; /** * http status-code 402: payment required. */ public static final int http_payment_required = 402; /** * http status-code 403: forbidden. */ public static final int http_forbidden = 403; /** * http status-code 404: not found. */ public static final int http_not_found = 404; /** * http status-code 405: method not allowed. * 方法不允许。例如,后端接口要求用get请求,前段用post请求 */ public static final int http_bad_method = 405; /** * http status-code 406: not acceptable. */ public static final int http_not_acceptable = 406; /** * http status-code 407: proxy authentication required. */ public static final int http_proxy_auth = 407; /** * http status-code 408: request time-out. */ public static final int http_client_timeout = 408; /** * http status-code 409: conflict. */ public static final int http_conflict = 409; /** * http status-code 410: gone. */ public static final int http_gone = 410; /** * http status-code 411: length required. */ public static final int http_length_required = 411; /** * http status-code 412: precondition failed. */ public static final int http_precon_failed = 412; /** * http status-code 413: request entity too large. */ public static final int http_entity_too_large = 413; /** * http status-code 414: request-uri too large. */ public static final int http_req_too_long = 414; /** * http status-code 415: unsupported media type. */ public static final int http_unsupported_type = 415;
4.2.5.服务错误——5xx: server error
/** * http status-code 500: internal server error. * @deprecated it is misplaced and shouldn't have existed. * 内部服务器错误,它放错地方了,不应该存在。 * (存在争议,不太确定是http_server_error不应该存在,还是@deprecated不应该存在) */ @deprecated public static final int http_server_error = 500; /** * http status-code 500: internal server error. * 内部服务器错误。 */ public static final int http_internal_error = 500; /** * http status-code 501: not implemented. */ public static final int http_not_implemented = 501; /** * http status-code 502: bad gateway. * 网关错误。 */ public static final int http_bad_gateway = 502; /** * http status-code 503: service unavailable. * 服务不可用 */ public static final int http_unavailable = 503; /** * http status-code 504: gateway timeout. * 网关超时。 */ public static final int http_gateway_timeout = 504; /** * http status-code 505: http version not supported. * 不支持http版本。 */ public static final int http_version = 505;
到此这篇关于springboot中的server.context-path的实现的文章就介绍到这了,更多相关springboot server.context-path内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论