@RequestMapping("/sendMsg",method = {RequestMethod.GET,RequestMethod.POST})
@ApiApplication(isAuth = false)
@ApiOperation("发送消息")
public AjaxResult senMsg(HttpServletRequest request) throws IOException {
String method = request.getMethod();
MessageParam param = new MessageParam();
if("POST".equals(method)){
String postData = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8);
param = JSONObject.parseObject(postData,MessageParam.class);
}
if("GET".equals(method)){
ServletRequestDataBinder binder = new ServletRequestDataBinder(param);
binder.bind(request);
}
if(StringUtils.isNotBlank(param.getImgUrl())){
if(!param.getImgUrl().contains("http")){
return AjaxResult.error("请输入正确的图片地址");
}
param.setImgUrl(URLEncoder.encode(param.getImgUrl()));
}
if(StringUtils.isBlank(param.getToken())){
return AjaxResult.error("token为空");
}
// 后续处理...
return AjaxResult.success("成功");
}
Springboot同时支持GET与POST请求及参数获取
于 2023-03-24 17:30:42 首次发布
该代码段定义了一个处理/sendMsg请求的API,支持GET和POST方法。它解析请求参数,校验图片URL和token,对图片URL进行编码,并在验证失败时返回错误信息。当所有验证成功后,会返回成功的响应。
摘要由CSDN通过智能技术生成