赞
踩
目录
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.
书接上回,SpringBoot 在 idea中的 .idea和 .iml文件-CSDN博客,我在boot-test的测试项目中使用的 SpringBoot版本为 1.3.5.RELEASE,新项目 cps-task中使用的版本为 2.4.8,造成了连接异常,问题很好解决,但涉及的bug记录一下。
首先,先看一张图片
- 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";
- }
- }
- server.port=8899
- server.context-path=/cps-task/
在Spring Boot项目中,server.context-path
属性用于设置应用程序的上下文路径(context path),这样应用就不会直接部署在根路径(/)下,而是部署在指定的路径下。然而,需要注意的是,从Spring Boot 2.0开始,server.context-path
属性已经被弃用,并被 server.servlet.context-path
所替代。
我这里在 2.4.8 的高版本中,使用了低版本的参数,造成参数未生效,所以,出现了404连接异常。
对于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/
java.net.HttpURLConnection;
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??
- /* 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;
Multiple Choices指当服务器接收到一个请求,并且该请求可以通过多种不同的方式或资源来满足时,服务器可能会返回300 Multiple Choices状态码。这意味着服务器提供了多个可能的响应,但由客户端来决定选择哪一个。
当客户端收到300 Multiple Choices状态码时,它应该显示一个包含所有可能响应选项的用户界面,让用户选择一个。然而,由于这个过程可能比较复杂且用户体验不佳,因此很少有网站会这样做。
- /**
- * HTTP Status-Code 300: Multiple Choices.
- */
- public static final int HTTP_MULT_CHOICE = 300;
Moved Permanently,是一个HTTP响应状态码,表示请求的资源已经被永久地移动到了由Location头部所指示的URL上。这是一个重定向状态码,它告诉客户端(如浏览器)自动地访问新的URL。
特点:
使用场景:
- /**
- * HTTP Status-Code 301: Moved Permanently.
- */
- public static final int HTTP_MOVED_PERM = 301;
-
Temporary Redirect,是一种HTTP重定向状态码,用于指示客户端发送的请求需要进一步重定向到另一个URL,但这次重定向是临时的。与301 Moved Permanently(永久重定向)不同,307 Temporary Redirect表明资源的移动是暂时的,客户端在将来可能会再次尝试访问原始URL。
特点:
使用场景:
- /**
- * HTTP Status-Code 302: Temporary Redirect.
- */
- public static final int HTTP_MOVED_TEMP = 302;
See Other,是一个HTTP重定向状态码,用于指示客户端使用GET方法向另一个URI(统一资源标识符)发送请求,以获取请求的资源。以下是关于303 See Other的详细解释:
使用场景:
- /**
- * HTTP Status-Code 303: See Other.
- */
- public static final int HTTP_SEE_OTHER = 303;
Not Modified,是一个HTTP响应状态码,用于指示客户端请求的资源自上次请求后没有发生改变,因此客户端可以继续使用其缓存的版本,而无需重新下载资源。以下是关于304 Not Modified的详细解释:
定义与特点:
使用场景:
- /**
- * HTTP Status-Code 304: Not Modified.
- */
- public static final int HTTP_NOT_MODIFIED = 304;
Use Proxy(使用代理)是一个常见的配置或行为,它涉及到通过代理服务器来转发HTTP请求和响应。代理服务器的作用
- /**
- * HTTP Status-Code 305: Use Proxy.
- */
- public static final int HTTP_USE_PROXY = 305;
- /**
- * 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;
- /**
- * 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;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。