新增restful模式

This commit is contained in:
六如
2025-02-04 12:12:41 +08:00
parent a2a438562b
commit f9a663d560
5 changed files with 26 additions and 2 deletions

View File

@@ -6,6 +6,8 @@ import com.sop.example.rest.examplerest.rest.GoodsController;
import com.sop.example.rest.examplerest.rest.vo.GoodsVO;
import org.apache.dubbo.config.annotation.DubboService;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -16,13 +18,26 @@ import java.util.Map;
public class GoodsControllerImpl implements GoodsController {
@Override
public GoodsVO getById(Integer id) {
WebContext webContext = WebContext.current();
GoodsVO goodsVO = new GoodsVO();
goodsVO.setId(id);
goodsVO.setName("冰箱");
Map<String, String> headers = WebContext.current().getHeaders();
List<String> list = new ArrayList<>();
list.add("realIp:" + webContext.getRealIp());
Map<String, String> headers = webContext.getHeaders();
String token = headers.get("token");
System.out.println(token);
list.add("token:" + token);
String remoteAddr = webContext.getRemoteAddr();
list.add("remoteAddr:" + remoteAddr);
String idValue = webContext.getParameter("id");
list.add("id:" + idValue);
goodsVO.setRemark(list.toString());
return goodsVO;
}
}

View File

@@ -12,4 +12,6 @@ public class GoodsVO {
private String name;
private String remark;
}

View File

@@ -119,6 +119,7 @@ public class ParamExecutorImpl implements ParamExecutor<HttpServletRequest, Http
defaultWebContext.setLocale(request.getLocale());
defaultWebContext.setHeaders(RequestUtil.getHeaders(request));
defaultWebContext.setParamtreMap(new LinkedHashMap<>(request.getParameterMap()));
defaultWebContext.setRealIp(RequestUtil.getIP(request));
return defaultWebContext;
}

View File

@@ -33,6 +33,7 @@ public class DefaultWebContext extends WebContext implements Serializable {
private Locale locale;
private Map<String, String> headers;
private Map<String, String[]> paramtreMap;
private String realIp;
@Override
public String getParameter(String name) {

View File

@@ -58,6 +58,11 @@ public abstract class WebContext {
*/
public abstract Map<String, String> getHeaders();
/**
* 获取真实ip地址
*/
public abstract String getRealIp();
protected void setContext(WebContext openContext) {
THREAD_LOCAL.set(openContext);
}