https://www.postman.com/downloads/

案例接口

postman 集合中添加


设置接口信息

1、配置接口并发送(输出html)

http://localhost:8085/product?action=search&name=a&page=1&size=2

\fjht-mall\src\main\java\com\fjhtxx\mall\user\servlet\ProductServlet.java

/**
     * 首页搜索商品
     *
     * @param req
     * @param resp
     */
    public void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
            IOException {
        // 搜索条件
        SearchDTO searchDTO = new SearchDTO();
        WebUtil.copyProperties(req, searchDTO);
        // 分页参数
        PageParam pageParam = new PageParam();
        WebUtil.copyProperties(req, pageParam);
        PageUtil pageUtil = productService.selectAllByKeyword(searchDTO, pageParam);
        // pageUtil == null,则说明没有查询到商品,展示商品空页面
        req.setAttribute("pageUtil", pageUtil);
        // 分类列表
        List<Category> categoryList = categoryService.getAll();
        req.setAttribute("search", searchDTO);
        req.setAttribute("categoryList", categoryList);
        req.getRequestDispatcher("/user/search/search.jsp").forward(req, resp);
    }

2、配置接口并发送(输出json)

\fjht-mall\src\main\java\com\fjhtxx\mall\user\servlet\ProductServlet.java

http://localhost:8085/product?action=getJson

Content-Type  application/json
   /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    public void getJson(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf8");
        response.setCharacterEncoding("utf8");
        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        JSONObject obj = new JSONObject();
        obj.set("message", "hello from server");
        out.print(obj);
    }

上面代码可能报错,需要引入对应的包


import java.io.PrintWriter;
import cn.hutool.json.JSONObject;


文档更新时间: 2022-12-22 07:16   作者:admin