优化Rest

This commit is contained in:
redkale
2023-11-07 23:27:52 +08:00
parent 8cbdcca40f
commit 7f8d6f96f1
8 changed files with 37 additions and 212 deletions

View File

@@ -83,7 +83,7 @@ public class HttpClusterRpcClient extends HttpRpcClient {
module = module.substring(1); //去掉/ module = module.substring(1); //去掉/
module = module.substring(0, module.indexOf('/')); module = module.substring(0, module.indexOf('/'));
Map<String, String> headers = req.getHeaders(); Map<String, String> headers = req.getHeaders();
String resname = headers == null ? "" : headers.getOrDefault(Rest.REST_HEADER_RESOURCE_NAME, ""); String resname = headers == null ? "" : headers.getOrDefault(Rest.REST_HEADER_RESNAME, "");
final String localModule = module; final String localModule = module;
if (logger.isLoggable(Level.FINEST)) { if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST, "httpAsync.queryHttpAddress: module=" + localModule + ", resname=" + resname); logger.log(Level.FINEST, "httpAsync.queryHttpAddress: module=" + localModule + ", resname=" + resname);
@@ -104,14 +104,11 @@ public class HttpClusterRpcClient extends HttpRpcClient {
if (isNotEmpty(req.getTraceid())) { if (isNotEmpty(req.getTraceid())) {
clientHeaders.put(Rest.REST_HEADER_TRACEID, req.getTraceid()); clientHeaders.put(Rest.REST_HEADER_TRACEID, req.getTraceid());
} }
if (req.isFrombody()) {
clientHeaders.put(Rest.REST_HEADER_PARAM_FROM_BODY, "true");
}
if (req.getReqConvertType() != null) { if (req.getReqConvertType() != null) {
clientHeaders.put(Rest.REST_HEADER_REQ_CONVERT_TYPE, req.getReqConvertType().toString()); clientHeaders.put(Rest.REST_HEADER_REQ_CONVERT, req.getReqConvertType().toString());
} }
if (req.getRespConvertType() != null) { if (req.getRespConvertType() != null) {
clientHeaders.put(Rest.REST_HEADER_RESP_CONVERT_TYPE, req.getRespConvertType().toString()); clientHeaders.put(Rest.REST_HEADER_RESP_CONVERT, req.getRespConvertType().toString());
} }
if (userid != null) { if (userid != null) {
clientHeaders.put(Rest.REST_HEADER_CURRUSERID, "" + userid); clientHeaders.put(Rest.REST_HEADER_CURRUSERID, "" + userid);
@@ -196,14 +193,14 @@ public class HttpClusterRpcClient extends HttpRpcClient {
// module = module.substring(1); //去掉/ // module = module.substring(1); //去掉/
// module = module.substring(0, module.indexOf('/')); // module = module.substring(0, module.indexOf('/'));
// Map<String, String> headers = req.getHeaders(); // Map<String, String> headers = req.getHeaders();
// String resname = headers == null ? "" : headers.getOrDefault(Rest.REST_HEADER_RESOURCE_NAME, ""); // String resname = headers == null ? "" : headers.getOrDefault(Rest.REST_HEADER_RESNAME, "");
// return clusterAgent.queryHttpAddress("http", module, resname).thenCompose(addrs -> { // return clusterAgent.queryHttpAddress("http", module, resname).thenCompose(addrs -> {
// if (addrs == null || addrs.isEmpty()) return new HttpResult().status(404).toAnyFuture(); // if (addrs == null || addrs.isEmpty()) return new HttpResult().status(404).toAnyFuture();
// java.net.http.HttpRequest.Builder builder = java.net.http.HttpRequest.newBuilder().timeout(Duration.ofMillis(30000)); // java.net.http.HttpRequest.Builder builder = java.net.http.HttpRequest.newBuilder().timeout(Duration.ofMillis(30000));
// if (req.isRpc()) builder.header(Rest.REST_HEADER_RPC_NAME, "true"); // if (req.isRpc()) builder.header(Rest.REST_HEADER_RPC_NAME, "true");
// if (req.isFrombody()) builder.header(Rest.REST_HEADER_PARAM_FROM_BODY, "true"); // if (req.isFrombody()) builder.header(Rest.REST_HEADER_PARAM_FROM_BODY, "true");
// if (req.getReqConvertType() != null) builder.header(Rest.REST_HEADER_REQ_CONVERT_TYPE, req.getReqConvertType().toString()); // if (req.getReqConvertType() != null) builder.header(Rest.REST_HEADER_REQ_CONVERT, req.getReqConvertType().toString());
// if (req.getRespConvertType() != null) builder.header(Rest.REST_HEADER_RESP_CONVERT_TYPE, req.getRespConvertType().toString()); // if (req.getRespConvertType() != null) builder.header(Rest.REST_HEADER_RESP_CONVERT, req.getRespConvertType().toString());
// if (userid != 0) builder.header(Rest.REST_HEADER_CURRUSERID, "" + userid); // if (userid != 0) builder.header(Rest.REST_HEADER_CURRUSERID, "" + userid);
// if (headers != null) headers.forEach((n, v) -> { // if (headers != null) headers.forEach((n, v) -> {
// if (!DISALLOWED_HEADERS_SET.contains(n.toLowerCase())) builder.header(n, v); // if (!DISALLOWED_HEADERS_SET.contains(n.toLowerCase())) builder.header(n, v);

View File

@@ -113,7 +113,7 @@ public abstract class HttpRpcClient implements ClusterRpcClient<HttpSimpleReques
module = module.substring(1); //去掉/ module = module.substring(1); //去掉/
module = module.substring(0, module.indexOf('/')); module = module.substring(0, module.indexOf('/'));
Map<String, String> headers = request.getHeaders(); Map<String, String> headers = request.getHeaders();
String resname = headers == null ? "" : headers.getOrDefault(Rest.REST_HEADER_RESOURCE_NAME, ""); String resname = headers == null ? "" : headers.getOrDefault(Rest.REST_HEADER_RESNAME, "");
return Rest.generateHttpReqTopic(module, resname, getNodeid()); return Rest.generateHttpReqTopic(module, resname, getNodeid());
} }

View File

@@ -46,7 +46,7 @@ public class HttpSimpleRequestCoder implements MessageCoder<HttpSimpleRequest> {
byte[] params = MessageCoder.getBytes(data.getParams()); byte[] params = MessageCoder.getBytes(data.getParams());
byte[] body = MessageCoder.getBytes(data.getBody()); byte[] body = MessageCoder.getBytes(data.getBody());
byte[] userid = MessageCoder.encodeUserid(data.getCurrentUserid()); byte[] userid = MessageCoder.encodeUserid(data.getCurrentUserid());
int count = 1 //rpc + frombody int count = 1 //rpc
+ 4 //reqConvertType + 4 //reqConvertType
+ 4 //respConvertType + 4 //respConvertType
+ 2 + traceid.length + 2 + traceid.length
@@ -60,7 +60,7 @@ public class HttpSimpleRequestCoder implements MessageCoder<HttpSimpleRequest> {
+ 4 + body.length; + 4 + body.length;
byte[] bs = new byte[count]; byte[] bs = new byte[count];
ByteBuffer buffer = ByteBuffer.wrap(bs); ByteBuffer buffer = ByteBuffer.wrap(bs);
buffer.put((byte) ((data.isRpc() ? 0b01 : 0) | (data.isFrombody() ? 0b10 : 0))); buffer.put((byte) (data.isRpc() ? 0b01 : 0));
buffer.putInt(data.getReqConvertType() == null ? 0 : data.getReqConvertType().getValue()); buffer.putInt(data.getReqConvertType() == null ? 0 : data.getReqConvertType().getValue());
buffer.putInt(data.getRespConvertType() == null ? 0 : data.getRespConvertType().getValue()); buffer.putInt(data.getRespConvertType() == null ? 0 : data.getRespConvertType().getValue());
buffer.putChar((char) traceid.length); buffer.putChar((char) traceid.length);
@@ -109,7 +109,6 @@ public class HttpSimpleRequestCoder implements MessageCoder<HttpSimpleRequest> {
HttpSimpleRequest req = new HttpSimpleRequest(); HttpSimpleRequest req = new HttpSimpleRequest();
byte opt = buffer.get(); byte opt = buffer.get();
req.setRpc((opt & 0b01) > 0); req.setRpc((opt & 0b01) > 0);
req.setFrombody((opt & 0b10) > 0);
int reqformat = buffer.getInt(); int reqformat = buffer.getInt();
int respformat = buffer.getInt(); int respformat = buffer.getInt();
if (reqformat != 0) { if (reqformat != 0) {

View File

@@ -113,9 +113,6 @@ public class HttpRequest extends Request<HttpContext> {
protected Supplier<Serializable> currentUserSupplier; protected Supplier<Serializable> currentUserSupplier;
//参数是否从body中取
protected boolean frombody;
protected ConvertType reqConvertType; protected ConvertType reqConvertType;
protected Convert reqConvert; protected Convert reqConvert;
@@ -212,7 +209,6 @@ public class HttpRequest extends Request<HttpContext> {
if (req.getHeaders() != null) { if (req.getHeaders() != null) {
this.headers.putAll(req.getHeaders()); this.headers.putAll(req.getHeaders());
} }
this.frombody = req.isFrombody();
this.reqConvertType = req.getReqConvertType(); this.reqConvertType = req.getReqConvertType();
this.reqConvert = req.getReqConvertType() == null ? null : ConvertFactory.findConvert(req.getReqConvertType()); this.reqConvert = req.getReqConvertType() == null ? null : ConvertFactory.findConvert(req.getReqConvertType());
this.respConvertType = req.getRespConvertType(); this.respConvertType = req.getRespConvertType();
@@ -323,7 +319,6 @@ public class HttpRequest extends Request<HttpContext> {
this.rpc = httplast.rpc; this.rpc = httplast.rpc;
this.traceid = httplast.traceid; this.traceid = httplast.traceid;
this.currentUserid = httplast.currentUserid; this.currentUserid = httplast.currentUserid;
this.frombody = httplast.frombody;
this.reqConvertType = httplast.reqConvertType; this.reqConvertType = httplast.reqConvertType;
this.reqConvert = httplast.reqConvert; this.reqConvert = httplast.reqConvert;
this.respConvertType = httplast.respConvertType; this.respConvertType = httplast.respConvertType;
@@ -823,19 +818,13 @@ public class HttpRequest extends Request<HttpContext> {
this.currentUserid = value; this.currentUserid = value;
headers.put(name, value); headers.put(name, value);
break; break;
case Rest.REST_HEADER_PARAM_FROM_BODY: //rest-param-from-body case Rest.REST_HEADER_REQ_CONVERT: //rest-req-convert-type
this.frombody = vlen == 4 && content[0] == 't' && content[1] == 'r' && content[2] == 'u' && content[3] == 'e';
headers.put(name, this.frombody ? "true"
: (vlen == 5 && content[0] == 'f' && content[1] == 'a' && content[2] == 'l' && content[3] == 's' && content[4] == 'e'
? "false" : bytes.toString(true, charset)));
break;
case Rest.REST_HEADER_REQ_CONVERT_TYPE: //rest-req-convert-type
value = bytes.toString(true, charset); value = bytes.toString(true, charset);
reqConvertType = ConvertType.valueOf(value); reqConvertType = ConvertType.valueOf(value);
reqConvert = ConvertFactory.findConvert(reqConvertType); reqConvert = ConvertFactory.findConvert(reqConvertType);
headers.put(name, value); headers.put(name, value);
break; break;
case Rest.REST_HEADER_RESP_CONVERT_TYPE: //rest-resp-convert-type case Rest.REST_HEADER_RESP_CONVERT: //rest-resp-convert-type
value = bytes.toString(true, charset); value = bytes.toString(true, charset);
respConvertType = ConvertType.valueOf(value); respConvertType = ConvertType.valueOf(value);
respConvert = ConvertFactory.findConvert(respConvertType); respConvert = ConvertFactory.findConvert(respConvertType);
@@ -948,7 +937,6 @@ public class HttpRequest extends Request<HttpContext> {
req.traceid = this.traceid; req.traceid = this.traceid;
req.currentUserid = this.currentUserid; req.currentUserid = this.currentUserid;
req.currentUserSupplier = this.currentUserSupplier; req.currentUserSupplier = this.currentUserSupplier;
req.frombody = this.frombody;
req.reqConvertType = this.reqConvertType; req.reqConvertType = this.reqConvertType;
req.reqConvert = this.reqConvert; req.reqConvert = this.reqConvert;
req.respConvert = this.respConvert; req.respConvert = this.respConvert;
@@ -985,7 +973,6 @@ public class HttpRequest extends Request<HttpContext> {
this.readState = READ_STATE_ROUTE; this.readState = READ_STATE_ROUTE;
this.currentUserid = CURRUSERID_NIL; this.currentUserid = CURRUSERID_NIL;
this.currentUserSupplier = null; this.currentUserSupplier = null;
this.frombody = false;
this.reqConvertType = null; this.reqConvertType = null;
this.reqConvert = null; this.reqConvert = null;
this.respConvert = jsonConvert; this.respConvert = jsonConvert;
@@ -1542,7 +1529,6 @@ public class HttpRequest extends Request<HttpContext> {
public String toString() { public String toString() {
parseBody(); parseBody();
return this.getClass().getSimpleName() + "{\r\n method: " + this.method + ", \r\n requestURI: " + this.requestURI return this.getClass().getSimpleName() + "{\r\n method: " + this.method + ", \r\n requestURI: " + this.requestURI
+ (this.frombody ? (", \r\n frombody: " + this.frombody) : "")
+ (this.reqConvertType != null ? (", \r\n reqConvertType: " + this.reqConvertType) : "") + (this.reqConvertType != null ? (", \r\n reqConvertType: " + this.reqConvertType) : "")
+ (this.respConvertType != null ? (", \r\n respConvertType: " + this.respConvertType) : "") + (this.respConvertType != null ? (", \r\n respConvertType: " + this.respConvertType) : "")
+ (this.currentUserid != CURRUSERID_NIL ? (", \r\n currentUserid: " + (this.currentUserid == CURRUSERID_NIL ? null : this.currentUserid)) : "") + (this.currentUserid != CURRUSERID_NIL ? (", \r\n currentUserid: " + (this.currentUserid == CURRUSERID_NIL ? null : this.currentUserid)) : "")
@@ -2566,16 +2552,6 @@ public class HttpRequest extends Request<HttpContext> {
* @return 参数值 * @return 参数值
*/ */
public String getParameter(String name) { public String getParameter(String name) {
if (this.frombody) {
if (array.isEmpty()) {
return null;
}
Convert convert = this.reqConvert;
if (convert == null) {
convert = jsonConvert;
}
return (String) convert.convertFrom(String.class, array.content());
}
parseBody(); parseBody();
return params.get(name); return params.get(name);
} }
@@ -2589,16 +2565,6 @@ public class HttpRequest extends Request<HttpContext> {
* @return 参数值 * @return 参数值
*/ */
public String getParameter(String name, String defaultValue) { public String getParameter(String name, String defaultValue) {
if (this.frombody) {
if (array.isEmpty()) {
return defaultValue;
}
Convert convert = this.reqConvert;
if (convert == null) {
convert = jsonConvert;
}
return (String) convert.convertFrom(String.class, array.content());
}
parseBody(); parseBody();
return params.getOrDefault(name, defaultValue); return params.getOrDefault(name, defaultValue);
} }
@@ -2613,19 +2579,6 @@ public class HttpRequest extends Request<HttpContext> {
* @return 参数值 * @return 参数值
*/ */
public <T> T getJsonParameter(java.lang.reflect.Type type, String name) { public <T> T getJsonParameter(java.lang.reflect.Type type, String name) {
if (this.frombody) {
if (array.isEmpty()) {
return null;
}
Convert convert = this.reqConvert;
if (convert == null) {
convert = jsonConvert;
}
if (type == byte[].class) {
return (T) array.getBytes();
}
return (T) convert.convertFrom(type, array.content());
}
String v = getParameter(name); String v = getParameter(name);
return v == null || v.isEmpty() ? null : jsonConvert.convertFrom(type, v); return v == null || v.isEmpty() ? null : jsonConvert.convertFrom(type, v);
} }
@@ -2654,16 +2607,6 @@ public class HttpRequest extends Request<HttpContext> {
* @return 参数值 * @return 参数值
*/ */
public boolean getBooleanParameter(String name, boolean defaultValue) { public boolean getBooleanParameter(String name, boolean defaultValue) {
if (this.frombody) {
if (array.isEmpty()) {
return defaultValue;
}
Convert convert = this.reqConvert;
if (convert == null) {
convert = jsonConvert;
}
return (boolean) convert.convertFrom(boolean.class, array.content());
}
parseBody(); parseBody();
String value = params.get(name); String value = params.get(name);
return value == null || value.length() == 0 ? defaultValue : Boolean.parseBoolean(value); return value == null || value.length() == 0 ? defaultValue : Boolean.parseBoolean(value);
@@ -2678,16 +2621,6 @@ public class HttpRequest extends Request<HttpContext> {
* @return 参数值 * @return 参数值
*/ */
public short getShortParameter(String name, short defaultValue) { public short getShortParameter(String name, short defaultValue) {
if (this.frombody) {
if (array.isEmpty()) {
return defaultValue;
}
Convert convert = this.reqConvert;
if (convert == null) {
convert = jsonConvert;
}
return (short) convert.convertFrom(short.class, array.content());
}
parseBody(); parseBody();
String value = params.get(name); String value = params.get(name);
if (value == null || value.length() == 0) { if (value == null || value.length() == 0) {
@@ -2710,16 +2643,6 @@ public class HttpRequest extends Request<HttpContext> {
* @return 参数值 * @return 参数值
*/ */
public short getShortParameter(int radix, String name, short defaultValue) { public short getShortParameter(int radix, String name, short defaultValue) {
if (this.frombody) {
if (array.isEmpty()) {
return (short) defaultValue;
}
Convert convert = this.reqConvert;
if (convert == null) {
convert = jsonConvert;
}
return (short) convert.convertFrom(short.class, array.content());
}
parseBody(); parseBody();
String value = params.get(name); String value = params.get(name);
if (value == null || value.length() == 0) { if (value == null || value.length() == 0) {
@@ -2741,16 +2664,6 @@ public class HttpRequest extends Request<HttpContext> {
* @return 参数值 * @return 参数值
*/ */
public short getShortParameter(String name, int defaultValue) { public short getShortParameter(String name, int defaultValue) {
if (this.frombody) {
if (array.isEmpty()) {
return (short) defaultValue;
}
Convert convert = this.reqConvert;
if (convert == null) {
convert = jsonConvert;
}
return (short) convert.convertFrom(short.class, array.content());
}
parseBody(); parseBody();
String value = params.get(name); String value = params.get(name);
if (value == null || value.length() == 0) { if (value == null || value.length() == 0) {
@@ -2772,16 +2685,6 @@ public class HttpRequest extends Request<HttpContext> {
* @return 参数值 * @return 参数值
*/ */
public int getIntParameter(String name, int defaultValue) { public int getIntParameter(String name, int defaultValue) {
if (this.frombody) {
if (array.isEmpty()) {
return defaultValue;
}
Convert convert = this.reqConvert;
if (convert == null) {
convert = jsonConvert;
}
return (int) convert.convertFrom(int.class, array.content());
}
parseBody(); parseBody();
String value = params.get(name); String value = params.get(name);
if (value == null || value.length() == 0) { if (value == null || value.length() == 0) {
@@ -2804,16 +2707,6 @@ public class HttpRequest extends Request<HttpContext> {
* @return 参数值 * @return 参数值
*/ */
public int getIntParameter(int radix, String name, int defaultValue) { public int getIntParameter(int radix, String name, int defaultValue) {
if (this.frombody) {
if (array.isEmpty()) {
return defaultValue;
}
Convert convert = this.reqConvert;
if (convert == null) {
convert = jsonConvert;
}
return (int) convert.convertFrom(int.class, array.content());
}
parseBody(); parseBody();
String value = params.get(name); String value = params.get(name);
if (value == null || value.length() == 0) { if (value == null || value.length() == 0) {
@@ -2835,16 +2728,6 @@ public class HttpRequest extends Request<HttpContext> {
* @return 参数值 * @return 参数值
*/ */
public long getLongParameter(String name, long defaultValue) { public long getLongParameter(String name, long defaultValue) {
if (this.frombody) {
if (array.isEmpty()) {
return defaultValue;
}
Convert convert = this.reqConvert;
if (convert == null) {
convert = jsonConvert;
}
return (long) convert.convertFrom(long.class, array.content());
}
parseBody(); parseBody();
String value = params.get(name); String value = params.get(name);
if (value == null || value.length() == 0) { if (value == null || value.length() == 0) {
@@ -2867,16 +2750,6 @@ public class HttpRequest extends Request<HttpContext> {
* @return 参数值 * @return 参数值
*/ */
public long getLongParameter(int radix, String name, long defaultValue) { public long getLongParameter(int radix, String name, long defaultValue) {
if (this.frombody) {
if (array.isEmpty()) {
return defaultValue;
}
Convert convert = this.reqConvert;
if (convert == null) {
convert = jsonConvert;
}
return (long) convert.convertFrom(long.class, array.content());
}
parseBody(); parseBody();
String value = params.get(name); String value = params.get(name);
if (value == null || value.length() == 0) { if (value == null || value.length() == 0) {
@@ -2898,16 +2771,6 @@ public class HttpRequest extends Request<HttpContext> {
* @return 参数值 * @return 参数值
*/ */
public float getFloatParameter(String name, float defaultValue) { public float getFloatParameter(String name, float defaultValue) {
if (this.frombody) {
if (array.isEmpty()) {
return defaultValue;
}
Convert convert = this.reqConvert;
if (convert == null) {
convert = jsonConvert;
}
return (float) convert.convertFrom(float.class, array.content());
}
parseBody(); parseBody();
String value = params.get(name); String value = params.get(name);
if (value == null || value.length() == 0) { if (value == null || value.length() == 0) {
@@ -2929,16 +2792,6 @@ public class HttpRequest extends Request<HttpContext> {
* @return 参数值 * @return 参数值
*/ */
public double getDoubleParameter(String name, double defaultValue) { public double getDoubleParameter(String name, double defaultValue) {
if (this.frombody) {
if (array.isEmpty()) {
return defaultValue;
}
Convert convert = this.reqConvert;
if (convert == null) {
convert = jsonConvert;
}
return (double) convert.convertFrom(double.class, array.content());
}
parseBody(); parseBody();
String value = params.get(name); String value = params.get(name);
if (value == null || value.length() == 0) { if (value == null || value.length() == 0) {

View File

@@ -30,14 +30,10 @@ import org.redkale.util.Traces;
*/ */
public class HttpSimpleRequest extends ClientRequest implements java.io.Serializable { public class HttpSimpleRequest extends ClientRequest implements java.io.Serializable {
@ConvertColumn(index = 1) @ConvertColumn(index = 2)
@Comment("是否RPC请求, 该类通常是为RPC创建的故默认是true") @Comment("是否RPC请求, 该类通常是为RPC创建的故默认是true")
protected boolean rpc = true; protected boolean rpc = true;
@ConvertColumn(index = 2)
@Comment("是否从body中获取参数比如protobuf数据格式")
protected boolean frombody;
@ConvertColumn(index = 3) @ConvertColumn(index = 3)
@Comment("链路ID") @Comment("链路ID")
protected String traceid; protected String traceid;
@@ -163,16 +159,6 @@ public class HttpSimpleRequest extends ClientRequest implements java.io.Serializ
return this; return this;
} }
public HttpSimpleRequest requestURI(boolean frombody) {
this.frombody = frombody;
return this;
}
public HttpSimpleRequest frombody(boolean frombody) {
this.frombody = frombody;
return this;
}
public HttpSimpleRequest bothConvertType(ConvertType convertType) { public HttpSimpleRequest bothConvertType(ConvertType convertType) {
this.reqConvertType = convertType; this.reqConvertType = convertType;
this.respConvertType = convertType; this.respConvertType = convertType;
@@ -464,14 +450,6 @@ public class HttpSimpleRequest extends ClientRequest implements java.io.Serializ
this.body = body; this.body = body;
} }
public boolean isFrombody() {
return frombody;
}
public void setFrombody(boolean frombody) {
this.frombody = frombody;
}
public ConvertType getReqConvertType() { public ConvertType getReqConvertType() {
return reqConvertType; return reqConvertType;
} }

View File

@@ -38,27 +38,25 @@ import org.redkale.util.*;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public final class Rest { public final class Rest {
//请求所需的RestService的资源名值类型: 字符串
public static final String REST_HEADER_RESOURCE_NAME = "rest-resource-name";
//请求是否为rpc协议值类型: 布尔取值为true、false //请求是否为rpc协议值类型: 布尔取值为true、false
public static final String REST_HEADER_RPC = "rest-rpc"; public static final String REST_HEADER_RPC = "Rest-Rpc";
//traceid值类型: 字符串 //traceid值类型: 字符串
public static final String REST_HEADER_TRACEID = "rest-traceid"; public static final String REST_HEADER_TRACEID = "Rest-Traceid";
//当前用户ID值值类型: 字符串 //当前用户ID值值类型: 字符串
public static final String REST_HEADER_CURRUSERID = "rest-curruserid"; public static final String REST_HEADER_CURRUSERID = "Rest-Curruserid";
//参数是否从body中获取值类型: 布尔取值为true、false //请求所需的RestService的资源名值类型: 字符串
public static final String REST_HEADER_PARAM_FROM_BODY = "rest-param-from-body"; public static final String REST_HEADER_RESNAME = "Rest-Resname";
//请求参数的反序列化种类,值类型: 字符串取值为ConvertType枚举值名 //请求参数的反序列化种类,值类型: 字符串取值为ConvertType枚举值名
public static final String REST_HEADER_REQ_CONVERT_TYPE = "rest-req-convert-type"; public static final String REST_HEADER_REQ_CONVERT = "Rest-Req-Convert";
//响应结果的序列化种类,值类型: 字符串取值为ConvertType枚举值名 //响应结果的序列化种类,值类型: 字符串取值为ConvertType枚举值名
public static final String REST_HEADER_RESP_CONVERT_TYPE = "rest-resp-convert-type"; public static final String REST_HEADER_RESP_CONVERT = "Rest-Resp-Convert";
//---------------------------------------------------------------------------------------------------
static final String REST_TOSTRINGOBJ_FIELD_NAME = "_redkale_tostringsupplier"; static final String REST_TOSTRINGOBJ_FIELD_NAME = "_redkale_tostringsupplier";
static final String REST_CONVERT_FIELD_PREFIX = "_redkale_restconvert_"; static final String REST_CONVERT_FIELD_PREFIX = "_redkale_restconvert_";
@@ -1914,7 +1912,7 @@ public final class Rest {
mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, newDynName, REST_SERVICEMAP_FIELD_NAME, "Ljava/util/Map;"); mv.visitFieldInsn(GETFIELD, newDynName, REST_SERVICEMAP_FIELD_NAME, "Ljava/util/Map;");
mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(ALOAD, 1);
mv.visitLdcInsn(REST_HEADER_RESOURCE_NAME); mv.visitLdcInsn(REST_HEADER_RESNAME);
mv.visitLdcInsn(""); mv.visitLdcInsn("");
mv.visitMethodInsn(INVOKEVIRTUAL, reqInternalName, "getHeader", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", false); mv.visitMethodInsn(INVOKEVIRTUAL, reqInternalName, "getHeader", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", false);
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;", true); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;", true);

View File

@@ -35,7 +35,7 @@ public class _DynHelloRestServlet1 extends SimpleRestServlet {
entity.setHelloname("my name"); entity.setHelloname("my name");
Map<String, String> headers = new HashMap<>(); Map<String, String> headers = new HashMap<>();
headers.put("hello-res", "my res"); headers.put("hello-res", "my res");
//headers.put(Rest.REST_HEADER_RESOURCE_NAME, "my-res"); //headers.put(Rest.REST_HEADER_RESNAME, "my-res");
String url = "http://127.0.0.1:" + port + "/pipes/hello/update?entity={}&bean2={}"; String url = "http://127.0.0.1:" + port + "/pipes/hello/update?entity={}&bean2={}";
System.out.println(Utility.postHttpContent(url, headers, null)); System.out.println(Utility.postHttpContent(url, headers, null));
url = "http://127.0.0.1:" + port + "/pipes/hello/update2?entity={}&bean2={}"; url = "http://127.0.0.1:" + port + "/pipes/hello/update2?entity={}&bean2={}";
@@ -61,7 +61,7 @@ public class _DynHelloRestServlet1 extends SimpleRestServlet {
@HttpMapping(url = "/hello/create", auth = false) @HttpMapping(url = "/hello/create", auth = false)
public void create(HttpRequest req, HttpResponse resp) throws IOException { public void create(HttpRequest req, HttpResponse resp) throws IOException {
HelloService service = _redkale_servicemap == null ? _redkale_service : _redkale_servicemap.get(req.getHeader(Rest.REST_HEADER_RESOURCE_NAME, "")); HelloService service = _redkale_servicemap == null ? _redkale_service : _redkale_servicemap.get(req.getHeader(Rest.REST_HEADER_RESNAME, ""));
HelloEntity bean = req.getJsonParameter(HelloEntity.class, "bean"); HelloEntity bean = req.getJsonParameter(HelloEntity.class, "bean");
bean.setClientaddr(req.getRemoteAddr()); bean.setClientaddr(req.getRemoteAddr());
bean.setResname(req.getHeader("hello-res")); bean.setResname(req.getHeader("hello-res"));
@@ -72,7 +72,7 @@ public class _DynHelloRestServlet1 extends SimpleRestServlet {
@HttpMapping(url = "/hello/delete/", auth = false) @HttpMapping(url = "/hello/delete/", auth = false)
public void delete(HttpRequest req, HttpResponse resp) throws IOException { public void delete(HttpRequest req, HttpResponse resp) throws IOException {
HelloService service = _redkale_servicemap == null ? _redkale_service : _redkale_servicemap.get(req.getHeader(Rest.REST_HEADER_RESOURCE_NAME, "")); HelloService service = _redkale_servicemap == null ? _redkale_service : _redkale_servicemap.get(req.getHeader(Rest.REST_HEADER_RESNAME, ""));
int id = Integer.parseInt(req.getRequstURILastPath()); int id = Integer.parseInt(req.getRequstURILastPath());
service.deleteHello(id); service.deleteHello(id);
resp.finishJson(RetResult.success()); resp.finishJson(RetResult.success());
@@ -80,7 +80,7 @@ public class _DynHelloRestServlet1 extends SimpleRestServlet {
@HttpMapping(url = "/hello/update", auth = false) @HttpMapping(url = "/hello/update", auth = false)
public void update(HttpRequest req, HttpResponse resp) throws IOException { public void update(HttpRequest req, HttpResponse resp) throws IOException {
HelloService service = _redkale_servicemap == null ? _redkale_service : _redkale_servicemap.get(req.getHeader(Rest.REST_HEADER_RESOURCE_NAME, "")); HelloService service = _redkale_servicemap == null ? _redkale_service : _redkale_servicemap.get(req.getHeader(Rest.REST_HEADER_RESNAME, ""));
String clientaddr = req.getRemoteAddr(); String clientaddr = req.getRemoteAddr();
HelloEntity bean = req.getJsonParameter(HelloEntity.class, "bean"); HelloEntity bean = req.getJsonParameter(HelloEntity.class, "bean");
bean.setClientaddr(req.getRemoteAddr()); bean.setClientaddr(req.getRemoteAddr());
@@ -91,7 +91,7 @@ public class _DynHelloRestServlet1 extends SimpleRestServlet {
@HttpMapping(url = "/hello/partupdate", auth = false) @HttpMapping(url = "/hello/partupdate", auth = false)
public void partupdate(HttpRequest req, HttpResponse resp) throws IOException { public void partupdate(HttpRequest req, HttpResponse resp) throws IOException {
HelloService service = _redkale_servicemap == null ? _redkale_service : _redkale_servicemap.get(req.getHeader(Rest.REST_HEADER_RESOURCE_NAME, "")); HelloService service = _redkale_servicemap == null ? _redkale_service : _redkale_servicemap.get(req.getHeader(Rest.REST_HEADER_RESNAME, ""));
HelloEntity bean = req.getJsonParameter(HelloEntity.class, "bean"); HelloEntity bean = req.getJsonParameter(HelloEntity.class, "bean");
bean.setClientaddr(req.getRemoteAddr()); bean.setClientaddr(req.getRemoteAddr());
bean.setResname(req.getHeader("hello-res")); bean.setResname(req.getHeader("hello-res"));
@@ -102,7 +102,7 @@ public class _DynHelloRestServlet1 extends SimpleRestServlet {
@HttpMapping(url = "/hello/query", auth = false) @HttpMapping(url = "/hello/query", auth = false)
public void query(HttpRequest req, HttpResponse resp) throws IOException { public void query(HttpRequest req, HttpResponse resp) throws IOException {
HelloService service = _redkale_servicemap == null ? _redkale_service : _redkale_servicemap.get(req.getHeader(Rest.REST_HEADER_RESOURCE_NAME, "")); HelloService service = _redkale_servicemap == null ? _redkale_service : _redkale_servicemap.get(req.getHeader(Rest.REST_HEADER_RESNAME, ""));
HelloBean bean = req.getJsonParameter(HelloBean.class, "bean"); HelloBean bean = req.getJsonParameter(HelloBean.class, "bean");
bean.setClientaddr(req.getRemoteAddr()); bean.setClientaddr(req.getRemoteAddr());
bean.setUseragent(req.getHeader("User-Agent")); bean.setUseragent(req.getHeader("User-Agent"));
@@ -115,7 +115,7 @@ public class _DynHelloRestServlet1 extends SimpleRestServlet {
@HttpMapping(url = "/hello/list", auth = false) @HttpMapping(url = "/hello/list", auth = false)
public void list(HttpRequest req, HttpResponse resp) throws IOException { public void list(HttpRequest req, HttpResponse resp) throws IOException {
HelloService service = _redkale_servicemap == null ? _redkale_service : _redkale_servicemap.get(req.getHeader(Rest.REST_HEADER_RESOURCE_NAME, "")); HelloService service = _redkale_servicemap == null ? _redkale_service : _redkale_servicemap.get(req.getHeader(Rest.REST_HEADER_RESNAME, ""));
HelloBean bean = req.getJsonParameter(HelloBean.class, "bean"); HelloBean bean = req.getJsonParameter(HelloBean.class, "bean");
bean.setClientaddr(req.getRemoteAddr()); bean.setClientaddr(req.getRemoteAddr());
bean.setUseragent(req.getHeader("User-Agent")); bean.setUseragent(req.getHeader("User-Agent"));
@@ -127,7 +127,7 @@ public class _DynHelloRestServlet1 extends SimpleRestServlet {
@HttpMapping(url = "/hello/find/", auth = false) @HttpMapping(url = "/hello/find/", auth = false)
public void find(HttpRequest req, HttpResponse resp) throws IOException { public void find(HttpRequest req, HttpResponse resp) throws IOException {
HelloService service = _redkale_servicemap == null ? _redkale_service : _redkale_servicemap.get(req.getHeader(Rest.REST_HEADER_RESOURCE_NAME, "")); HelloService service = _redkale_servicemap == null ? _redkale_service : _redkale_servicemap.get(req.getHeader(Rest.REST_HEADER_RESNAME, ""));
int id = Integer.parseInt(req.getRequstURILastPath()); int id = Integer.parseInt(req.getRequstURILastPath());
HelloEntity bean = service.findHello(id); HelloEntity bean = service.findHello(id);
resp.finishJson(bean); resp.finishJson(bean);
@@ -135,21 +135,21 @@ public class _DynHelloRestServlet1 extends SimpleRestServlet {
@HttpMapping(url = "/hello/asyncfind/", auth = false) @HttpMapping(url = "/hello/asyncfind/", auth = false)
public void asyncfind(HttpRequest req, HttpResponse resp) throws IOException { public void asyncfind(HttpRequest req, HttpResponse resp) throws IOException {
HelloService service = _redkale_servicemap == null ? _redkale_service : _redkale_servicemap.get(req.getHeader(Rest.REST_HEADER_RESOURCE_NAME, "")); HelloService service = _redkale_servicemap == null ? _redkale_service : _redkale_servicemap.get(req.getHeader(Rest.REST_HEADER_RESNAME, ""));
int id = Integer.parseInt(req.getRequstURILastPath()); int id = Integer.parseInt(req.getRequstURILastPath());
resp.finishJson(service.asyncFindHello(id)); resp.finishJson(service.asyncFindHello(id));
} }
@HttpMapping(url = "/hello/asyncfind2/", auth = false) @HttpMapping(url = "/hello/asyncfind2/", auth = false)
public void asyncfind2(HttpRequest req, HttpResponse resp) throws IOException { public void asyncfind2(HttpRequest req, HttpResponse resp) throws IOException {
HelloService service = _redkale_servicemap == null ? _redkale_service : _redkale_servicemap.get(req.getHeader(Rest.REST_HEADER_RESOURCE_NAME, "")); HelloService service = _redkale_servicemap == null ? _redkale_service : _redkale_servicemap.get(req.getHeader(Rest.REST_HEADER_RESNAME, ""));
int id = Integer.parseInt(req.getRequstURILastPath()); int id = Integer.parseInt(req.getRequstURILastPath());
service.asyncFindHello(resp.createAsyncHandler(), id); service.asyncFindHello(resp.createAsyncHandler(), id);
} }
@HttpMapping(url = "/hello/asyncfind3/", auth = false) @HttpMapping(url = "/hello/asyncfind3/", auth = false)
public void asyncfind3(HttpRequest req, HttpResponse resp) throws IOException { public void asyncfind3(HttpRequest req, HttpResponse resp) throws IOException {
HelloService service = _redkale_servicemap == null ? _redkale_service : _redkale_servicemap.get(req.getHeader(Rest.REST_HEADER_RESOURCE_NAME, "")); HelloService service = _redkale_servicemap == null ? _redkale_service : _redkale_servicemap.get(req.getHeader(Rest.REST_HEADER_RESNAME, ""));
int id = Integer.parseInt(req.getRequstURILastPath()); int id = Integer.parseInt(req.getRequstURILastPath());
service.asyncFindHello(resp.createAsyncHandler(HelloAsyncHandler.class), id); service.asyncFindHello(resp.createAsyncHandler(HelloAsyncHandler.class), id);
} }

View File

@@ -29,7 +29,7 @@ public class _DynHelloRestServlet2 extends SimpleRestServlet {
@HttpMapping(url = "/hello/create", auth = false, comment = "创建Hello对象") @HttpMapping(url = "/hello/create", auth = false, comment = "创建Hello对象")
@HttpParam(name = "bean", type = HelloEntity.class, comment = "Hello对象") @HttpParam(name = "bean", type = HelloEntity.class, comment = "Hello对象")
public void create(HttpRequest req, HttpResponse resp) throws IOException { public void create(HttpRequest req, HttpResponse resp) throws IOException {
HelloService2 service = _redkale_servicemap == null ? _redkale_service : _redkale_servicemap.get(req.getHeader(Rest.REST_HEADER_RESOURCE_NAME, "")); HelloService2 service = _redkale_servicemap == null ? _redkale_service : _redkale_servicemap.get(req.getHeader(Rest.REST_HEADER_RESNAME, ""));
HelloEntity bean = req.getJsonParameter(HelloEntity.class, "bean"); HelloEntity bean = req.getJsonParameter(HelloEntity.class, "bean");
bean.setClientaddr(req.getRemoteAddr()); bean.setClientaddr(req.getRemoteAddr());
bean.setResname(req.getHeader("hello-res")); bean.setResname(req.getHeader("hello-res"));
@@ -43,7 +43,7 @@ public class _DynHelloRestServlet2 extends SimpleRestServlet {
@HttpMapping(url = "/hello/delete/", auth = false, comment = "根据id删除Hello对象") @HttpMapping(url = "/hello/delete/", auth = false, comment = "根据id删除Hello对象")
@HttpParam(name = "#", type = int.class, comment = "Hello对象id") @HttpParam(name = "#", type = int.class, comment = "Hello对象id")
public void delete(HttpRequest req, HttpResponse resp) throws IOException { public void delete(HttpRequest req, HttpResponse resp) throws IOException {
HelloService2 service = _redkale_servicemap == null ? _redkale_service : _redkale_servicemap.get(req.getHeader(Rest.REST_HEADER_RESOURCE_NAME, "")); HelloService2 service = _redkale_servicemap == null ? _redkale_service : _redkale_servicemap.get(req.getHeader(Rest.REST_HEADER_RESNAME, ""));
int id = Integer.parseInt(req.getRequstURILastPath()); int id = Integer.parseInt(req.getRequstURILastPath());
service.deleteHello(id); service.deleteHello(id);
resp.finishJson(RetResult.success()); resp.finishJson(RetResult.success());
@@ -52,7 +52,7 @@ public class _DynHelloRestServlet2 extends SimpleRestServlet {
@HttpMapping(url = "/hello/update", auth = false, comment = "修改Hello对象") @HttpMapping(url = "/hello/update", auth = false, comment = "修改Hello对象")
@HttpParam(name = "bean", type = HelloEntity.class, comment = "Hello对象") @HttpParam(name = "bean", type = HelloEntity.class, comment = "Hello对象")
public void update(HttpRequest req, HttpResponse resp) throws IOException { public void update(HttpRequest req, HttpResponse resp) throws IOException {
HelloService2 service = _redkale_servicemap == null ? _redkale_service : _redkale_servicemap.get(req.getHeader(Rest.REST_HEADER_RESOURCE_NAME, "")); HelloService2 service = _redkale_servicemap == null ? _redkale_service : _redkale_servicemap.get(req.getHeader(Rest.REST_HEADER_RESNAME, ""));
HelloEntity bean = req.getJsonParameter(HelloEntity.class, "bean"); HelloEntity bean = req.getJsonParameter(HelloEntity.class, "bean");
bean.setClientaddr(req.getRemoteAddr()); bean.setClientaddr(req.getRemoteAddr());
bean.setResname(req.getHeader("hello-res")); bean.setResname(req.getHeader("hello-res"));
@@ -65,7 +65,7 @@ public class _DynHelloRestServlet2 extends SimpleRestServlet {
@HttpMapping(url = "/hello/query", auth = false, comment = "查询Hello对象列表") @HttpMapping(url = "/hello/query", auth = false, comment = "查询Hello对象列表")
@HttpParam(name = "bean", type = HelloBean.class, comment = "过滤条件") @HttpParam(name = "bean", type = HelloBean.class, comment = "过滤条件")
public void query(HttpRequest req, HttpResponse resp) throws IOException { public void query(HttpRequest req, HttpResponse resp) throws IOException {
HelloService2 service = _redkale_servicemap == null ? _redkale_service : _redkale_servicemap.get(req.getHeader(Rest.REST_HEADER_RESOURCE_NAME, "")); HelloService2 service = _redkale_servicemap == null ? _redkale_service : _redkale_servicemap.get(req.getHeader(Rest.REST_HEADER_RESNAME, ""));
HelloBean bean = req.getJsonParameter(HelloBean.class, "bean"); HelloBean bean = req.getJsonParameter(HelloBean.class, "bean");
bean.setClientaddr(req.getRemoteAddr()); bean.setClientaddr(req.getRemoteAddr());
bean.setUseragent(req.getHeader("User-Agent")); bean.setUseragent(req.getHeader("User-Agent"));
@@ -79,7 +79,7 @@ public class _DynHelloRestServlet2 extends SimpleRestServlet {
@HttpMapping(url = "/hello/find/", auth = false, comment = "根据id删除Hello对象") @HttpMapping(url = "/hello/find/", auth = false, comment = "根据id删除Hello对象")
@HttpParam(name = "#", type = int.class, comment = "Hello对象id") @HttpParam(name = "#", type = int.class, comment = "Hello对象id")
public void find(HttpRequest req, HttpResponse resp) throws IOException { public void find(HttpRequest req, HttpResponse resp) throws IOException {
HelloService2 service = _redkale_servicemap == null ? _redkale_service : _redkale_servicemap.get(req.getHeader(Rest.REST_HEADER_RESOURCE_NAME, "")); HelloService2 service = _redkale_servicemap == null ? _redkale_service : _redkale_servicemap.get(req.getHeader(Rest.REST_HEADER_RESNAME, ""));
int id = Integer.parseInt(req.getRequstURILastPath()); int id = Integer.parseInt(req.getRequstURILastPath());
HelloEntity bean = service.findHello(id); HelloEntity bean = service.findHello(id);
resp.finishJson(bean); resp.finishJson(bean);