buildMessage
时间: 2023-10-28 16:01:00 浏览: 9
buildMessage是一个方法,用于构建提示消息。它接受一个格式化字符串和可变参数作为输入,然后根据参数的数量和格式化字符串生成消息。如果参数为空,则直接使用格式化字符串作为消息。该方法还通过获取调用该方法的堆栈跟踪信息,确定调用者的类和方法。最后,它返回一个格式化的消息,包括线程ID、调用者和消息内容。
相关问题
import okhttp3.HttpUrl; import java.security.Signature; import java.util.Base64; // Authorization: <schema> <token> // GET - getToken("GET", httpurl, "") // POST - getToken("POST", httpurl, json) String schema = "WECHATPAY2-SHA256-RSA2048"; HttpUrl httpurl = HttpUrl.parse(url); String getToken(String method, HttpUrl url, String body) { String nonceStr = "your nonce string"; long timestamp = System.currentTimeMillis() / 1000; String message = buildMessage(method, url, timestamp, nonceStr, body); String signature = sign(message.getBytes("utf-8")); return "mchid=\"" + yourMerchantId + "\"," + "nonce_str=\"" + nonceStr + "\"," + "timestamp=\"" + timestamp + "\"," + "serial_no=\"" + yourCertificateSerialNo + "\"," + "signature=\"" + signature + "\""; } String sign(byte[] message) { Signature sign = Signature.getInstance("SHA256withRSA"); sign.initSign(yourPrivateKey); sign.update(message); return Base64.getEncoder().encodeToString(sign.sign()); } String buildMessage(String method, HttpUrl url, long timestamp, String nonceStr, String body) { String canonicalUrl = url.encodedPath(); if (url.encodedQuery() != null) { canonicalUrl += "?" + url.encodedQuery(); } return method + "\n" + canonicalUrl + "\n" + timestamp + "\n" + nonceStr + "\n" + body + "\n";
这段代码是一个用于生成微信支付的授权信息的工具类。它使用了OkHttp库来构建HTTP请求的URL,生成签名所需的消息,并使用SHA256withRSA算法对消息进行签名。
代码中的`getToken`方法接收HTTP方法、URL、请求体作为参数,生成授权信息并返回。其中,nonceStr是一个随机字符串,timestamp是当前时间戳,message是根据请求信息构建的消息,signature是对消息进行签名后的结果。
`sign`方法使用了SHA256withRSA算法对消息进行签名,使用私钥进行初始化,并返回Base64编码后的签名结果。
`buildMessage`方法根据请求信息构建消息,包括HTTP方法、URL、时间戳、随机字符串和请求体。
这段代码需要你提供相关的参数,如商户ID、证书序列号和私钥等。你需要根据具体的微信支付接口文档来填充这些参数。
package com.bolt.gateway.handler; import com.bolt.gateway.provider.ResponseProvider; import org.springframework.boot.autoconfigure.web.ErrorProperties; import org.springframework.boot.autoconfigure.web.ResourceProperties; import org.springframework.boot.autoconfigure.web.reactive.error.DefaultErrorWebExceptionHandler; import org.springframework.boot.web.reactive.error.ErrorAttributes; import org.springframework.cloud.gateway.support.NotFoundException; import org.springframework.context.ApplicationContext; import org.springframework.http.HttpStatus; import org.springframework.web.reactive.function.server.*; import org.springframework.web.server.ResponseStatusException; import java.util.Map; /** * 异常处理 * * @author arch_group */ public class ErrorExceptionHandler extends DefaultErrorWebExceptionHandler { public ErrorExceptionHandler(ErrorAttributes errorAttributes, ResourceProperties resourceProperties, ErrorProperties errorProperties, ApplicationContext applicationContext) { super(errorAttributes, resourceProperties, errorProperties, applicationContext); } /** * 获取异常属性 */ @Override protected Map<String, Object> getErrorAttributes(ServerRequest request, boolean includeStackTrace) { int code = 500; Throwable error = super.getError(request); if (error instanceof NotFoundException) { code = 404; } if (error instanceof ResponseStatusException) { code = ((ResponseStatusException) error).getStatus().value(); } return ResponseProvider.response(code, this.buildMessage(request, error)); } /** * 指定响应处理方法为JSON处理的方法 * * @param errorAttributes */ @Override protected RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes errorAttributes) { return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse); } /** * 根据code获取对应的HttpStatus * * @param errorAttributes */ @Override protected HttpStatus getHttpStatus(Map<String, Object> errorAttributes) { int statusCode = (int) errorAttributes.get("code"); return HttpStatus.valueOf(statusCode); } /** * 构建异常信息 * * @param request * @param ex * @return */ private String buildMessage(ServerRequest request, Throwable ex) { StringBuilder message = new StringBuilder("Failed to handle request ["); message.append(request.methodName()); message.append(" "); message.append(request.uri()); message.append("]"); if (ex != null) { message.append(": "); message.append(ex.getMessage()); } return message.toString(); } }
这是一个自定义的异常处理类,用于处理Spring Cloud Gateway中的异常情况。这个类继承自`DefaultErrorWebExceptionHandler`,并重写了一些方法来自定义异常处理的逻辑。
在这个类中,主要做了以下几件事情:
1. 重写`getErrorAttributes`方法:根据不同的异常类型,设置不同的响应状态码和错误信息。如果是`NotFoundException`异常,设置状态码为404;如果是`ResponseStatusException`异常,获取其对应的HTTP状态码。
2. 重写`getRoutingFunction`方法:指定响应处理方法为JSON处理的方法。
3. 重写`getHttpStatus`方法:根据错误属性中的`code`获取对应的HTTP状态码。
4. 定义了一个私有方法`buildMessage`:用于构建异常信息,包括请求方法、URI和异常消息。
通过重写这些方法,我们可以自定义异常处理的逻辑,包括设置响应状态码和错误信息。如果你还有其他问题,请继续提问。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)