Use SpringBoot to read the basic content of HTTP Request SpringBoot using httprequest

2023-03-18  

When using SpringBoot to be familiar with the basic content of HTTP, he made a very simple Demo and transform the content in an HTTP request to print it into string format.

code is as follows:

Among them:

1. IOUTILS, used to convert the inputStream into spring;

2. @Responsebody annotation is used to return the processing results. If not, the request page will report errors that do not support the “get” or “post” method.

3. @Controller, with Controller, this class will be scanned;

4. @SpringBootApplication: On the startup class of SpringBoot, you need to add @SpringBootaapplication to scan other categories in the same root directory;

    @SpringBootApplication = @Controller + @EnableAutoConfiguration + @ComponentScan

5. @Requestermapping Annotation is used to map different URI, refer to the annotation itself of RequestMapping itself;

6. You can use Postman to send analog request to test this part of the code.

————————————————– — Dividing line———————————————- —

package helloworld.controller;

import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

@Controller
public class RequestController {
    @RequestMapping("/david")
    @ResponseBody
    String david() {
        return "Hello, David!";
    }

    @RequestMapping(value = "/angus/**", method = RequestMethod.GET)
    @ResponseBody
    public String getProcess(HttpServletRequest request){
        String retrunValue = "Hello, Angus! This is GET request!";
        System.out.println("=======GET Process=======");

        Map<String,String[]> requestMsg = request.getParameterMap();
        Enumeration<String> requestHeader = request.getHeaderNames();

        System.out.println("------- header -------");
        while(requestHeader.hasMoreElements()){
            String headerKey=requestHeader.nextElement().toString();
            // Print all header values
            System.out.println("headerKey="+headerKey+";value="+request.getHeader(headerKey));
        }

        System.out.println("------- parameter -------");
        for(String key :requestMsg.keySet())
        {
            for(int i=0;i<requestMsg.get(key).length;i++)
            {
                // Print all request parameter values
                System.out.println("key="+key+";value="+requestMsg.get(key)[i].toString());
            }
        }
        return retrunValue;
    }

    @RequestMapping(value = "/angus/**", method = RequestMethod.POST)
    @ResponseBody
    public String postProcess(HttpServletRequest request){
        String retrunValue = "Hello, Angus! This is POST Request!";
        System.out.println("=======POST Process=======");


        Map<String,String[]> requestMsg = request.getParameterMap();
        Enumeration<String> requestHeader = request.getHeaderNames();
        InputStream io = null;
        String body;
        System.out.println("------- body -------");
        try{
            io = request.getInputStream();
            body = IOUtils.toString(io);
            // Print Body content            System.out.println("Request Body="+body);
        }catch(IOException e){
            e.printStackTrace();
        }

        System.out.println("------- header -------");
        while(requestHeader.hasMoreElements()){
            String headerKey=requestHeader.nextElement().toString();
            // Print all header values
            System.out.println("headerKey="+headerKey+";value="+request.getHeader(headerKey));
        }

        System.out.println("------- parameters -------");
        for(String key :requestMsg.keySet())
        {
            for(int i=0;i<requestMsg.get(key).length;i++)
            {
                // Print all request parameter values                System.out.println("key="+key+";value="+requestMsg.get(key)[i].toString());
            }
        }
        return retrunValue;
    }
}

source

Random Posts

DP Getting Started Topics 2017-7-26

programmer’s homestead code to display garbled reasons

PANDAS MAP Applymap Apply method detailed explanation

TensorFlow 2.0.0 (TensorFlow1.xx can also be used)+ Kears test, entry routine

Absolutely do not handle the array in polymorphism