Content-Type 与 springmvc 参数注解的对应

package com.example.demo.controller;


import org.springframework.web.bind.annotation.*;

/**
 * @Classname AController
 * @Description
 * @Date 2021/7/2 14:57
 * @Created by xiaodu
 */
@RestController
public class AController {

    /**
     * multipart/form-data;
     * @param user
     * @return
     */
    @PostMapping("/formData")
    public User formData(@ModelAttribute(value = "user") User user){
        return user;
    }
    /**
     * multipart/form-data;
     * @param name
     * @param age
     * @return
     */
    @PostMapping("/formData1")
    public User formData1(@ModelAttribute(value = "name") String name,@ModelAttribute(value = "age") String age){
        return new User(name,age);
    }

    /**
     * 1 multipart/form-data;
     * 2 application/x-www-form-urlencoded
     * 3 http://localhost:8080/param1?name=9&age=7
     * @param name
     * @param age
     * @return
     */
    @PostMapping("/param1")
    public User param1(@RequestParam(value = "name") String name,
                       @RequestParam(value = "age") String age,
                       @RequestHeader(value = "Content-Type",required = false)String contentType){
        User user = new User(name, age);
        user.setContentType(contentType);
        return user;
    }

    /**
     * application/json
     * @param user
     * @return
     */
    @GetMapping("/json")
    public User json(@RequestBody User user){
        return user;
    }


}

 

posted @ 2021-07-02 14:15  蟑螂恶霸的开发笔记  阅读(115)  评论(0编辑  收藏  举报