Servlet gets json from request and returns json

To use json, you need to import fastjson-1.1.34.jar, official website download address: https://sourceforge.net/projects/fastjson/files/

Encapsulate a tool class that handles http requests to obtain json parameters:

package test;

import com.alibaba.fastjson.JSONObject;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class HttpGetJson {
    
    
    public static JSONObject getJson(HttpServletRequest request){
    
    
        StringBuilder result = new StringBuilder();
        BufferedReader in = null;
        try{
    
    
            in = new BufferedReader(new InputStreamReader(request.getInputStream(),StandardCharsets.UTF_8));
            String line;
            while((line = in.readLine()) != null){
    
    
                result.append(line);
            }
        }catch(IOException e){
    
    
            e.printStackTrace();
        }finally{
    
    
            try{
    
    
                if(in != null){
    
    
                    in.close();
                }
            }catch(IOException e){
    
    
                e.printStackTrace();
            }
        }
        return (JSONObject) JSONObject.parse(result.toString());
    }
}

Get the json data of the request in the doGet or doPost method of the Servlet :

JSONObject data = HttpGetJson.getJson(request);
        if(data != null) {
    
    
            //业务处理,例如:System.out.println("id-->" + data.get("id"));
            //利用get就能从json取出对应的键值对。
        }

After processing the business, if you need to return json:

JSONObject res = new JSONObject();
res.put("code",200);
res.put("id",id);
response.getWriter().write(String.valueOf(res));

In this way, the Servlet can get json from the request and return json.
doPost code example:

package test;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import com.alibaba.fastjson.JSONObject;

@WebServlet("/Demo")
public class Demo extends HttpServlet {
    
    
    protected void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException {
    
    
        response.setContentType("application/json;charset=utf-8");
        //获取json数据
        JSONObject data = HttpGetJson.getJson(request);
        Object id = null;
        Object name = null;
        Object status = null;
        int flag = 500;
        if(data != null) {
    
    
            id = data.get("id");
            name = data.get("name");
            status = data.get("status");
            flag = 200;
        }
        //返回json数据
        JSONObject res = new JSONObject();
        res.put("code",flag);
        res.put("id",id);
        res.put("name",name);
        res.put("status",status);
        response.getWriter().write(String.valueOf(res));
    }
}

Guess you like

Origin blog.csdn.net/weixin_56175092/article/details/126346904