南京h5网站开发,怎么提高网站seo优化关键字排名,哈尔滨百度搜索排名优化,小程序数据库怎么建立泛微e9开发 前端请求后端接口以及后端发布接口
前端请求后端接口 前端发起get请求 fetch(/api/youpath, {method: GET, // 默认 GET 方法#xff0c;可以省略headers: {Content-Type: application/json, // 通常 GET 请求无需指定 body#xff0c;Content-Type 不太重要},…泛微e9开发 前端请求后端接口以及后端发布接口
前端请求后端接口 前端发起get请求 fetch(/api/youpath, {method: GET, // 默认 GET 方法可以省略headers: {Content-Type: application/json, // 通常 GET 请求无需指定 bodyContent-Type 不太重要},
}).then(response response.json()).then(data console.log(data)).catch(error console.error(Error:, error));前端发起post请求 fetch(/api/youpath, {method: POST, // 指定请求方法为 POSTheaders: {Content-Type: application/json, // 指定请求体格式为 JSON},body: JSON.stringify({ // 将 JavaScript 对象转为 JSON 字符串name: John Doe,age: 30}),
}).then(response response.json()) // 解析 JSON 响应.then(data console.log(data)) // 处理响应数据.catch(error console.error(Error:, error)); // 错误处理后端发布接口
首先这个后端接口的路径必须是在com/api这个目录的下面 get类型的接口 package com.api.customization.kq.web;import com.alibaba.fastjson.JSON;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;Path(/customization/hrm/kq)
public class CardReplacementAction {GETPath(/card/getNumber)Produces(MediaType.APPLICATION_JSON)public String intervene(QueryParam(userid)int userid, QueryParam(date)String date) {BaseBean baseBean new BaseBean();HashMapString, Object result new HashMap();// 接受参数调用方法RecordSet recordSet new RecordSet();String sql select * from formtable_main_26 where sqr userid and sqrq like date.substring(0, 7)%;recordSet.executeQuery(sql);try {result.put(number, recordSet.getArray().size());}catch (Exception e) {baseBean.writeLog(lzlerrore.getMessage());}return JSON.toJSONString(result);}
}post 类型的接口 方法一通过从request对象中读取参数值
POST
Path(/getSomethingList)
Produces(MediaType.TEXT_PLAIN)
public String List(Context HttpServletRequest request, Context HttpServletResponse response) {MapString, Object apidatas new HashMapString, Object();String requestname Util.null2String(request.getParameter(requestname));String workflowid Util.null2String(request.getParameter(workflowid));apidatas.put(status, 1);return JSONObject.toJSONString(apidatas);
}方法二用类接收参数
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;// 定义路径
Path(/users)
public class UserResource {// 接收 POST 请求POSTConsumes(MediaType.APPLICATION_JSON) // 接收 JSON 数据Produces(MediaType.APPLICATION_JSON) // 返回 JSON 数据public Response createUser(User user) {// 这里JAX-RS 自动将 JSON 请求体解析为 User 对象// 打印接收到的用户信息System.out.println(Received user: user.getName() , user.getAge());// 返回成功响应并返回用户对象return Response.status(201) // HTTP 201 状态码资源已创建.entity(user) // 将用户对象作为响应返回.build();}
}注意事项
首先接口必须发布在com.api包下前端访问后端接口路径的时候需要在你编写的接口路径前加上/api比如访问上面的post接口就得访问/api/users