SpringBoot 接收请求的几种方式

package com.miaoshaproject.controller;


import com.alibaba.fastjson.JSONObject;
import com.miaoshaproject.dataobject.UserDO;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.Map;

@RestController
@RequestMapping("/GetParam")
public class TestGetParam  extends  BaseController{


    public  static final   String CONTENT_TYPE_FORMED= "application/x-www-form-urlencoded";
    public  static final   String CONTENT_TYPE_FORMED_JSON= "application/json;charset=UTF-8";


    @RequestMapping(value = "/login", method = {RequestMethod.POST},consumes = {CONTENT_TYPE_FORMED})
    @ResponseBody
    public  Map  testHttpServletRequest(HttpServletRequest request){
        Map map = new LinkedHashMap<>();
        map.put("status","0");
        map.put("data",request.getParameter("username"));
        return map;
    }

    //json.stringify()
    @RequestMapping(value = "/json", method = {RequestMethod.POST},consumes = {CONTENT_TYPE_FORMED_JSON})
    @ResponseBody
    public  Map testJson(@RequestBody JSONObject jsonParam){
        Map map = new LinkedHashMap<>();
        map.put("status",0);
        map.put("data",jsonParam.get("username"));
        return  map;
    }


    @RequestMapping(value = "/bean", method = {RequestMethod.POST},consumes = {CONTENT_TYPE_FORMED})
    @ResponseBody
    public  Map bean(UserDO userDO){
        Map map = new LinkedHashMap<>();
        map.put("status",0);
        map.put("data",userDO.getName());
        return  map;

    }

    @RequestMapping(value = "/Variable/{username}", method = {RequestMethod.POST},consumes = {CONTENT_TYPE_FORMED})
    @ResponseBody
    public  Map PathVariable(@PathVariable String username){
        Map map = new LinkedHashMap<>();
        map.put("status",0);
        map.put("data",username);
        return  map;
    }

    @RequestMapping(value = "/RequestParam", method = {RequestMethod.POST},consumes = {CONTENT_TYPE_FORMED})
    @ResponseBody
    public Map RequestParam(@RequestParam("username") String username){
        map.put("status",0);
        map.put("data",username);
        return  map;
    }

    @RequestMapping(value = "/direct", method = {RequestMethod.POST},consumes = {CONTENT_TYPE_FORMED})
    @ResponseBody
    public  Map direct(String username){
        map.put("status",0);
        map.put("data",username);
        return  map;
    }




}

 

你可能感兴趣的:(项目搭建,后端,配置,springBoot)