spring boot报Unsupported Media Type Content type '*/*;charset=UTF-8' not supported

1.请求设置Content-Type:application/json即可

ajax一般默认:Content-Type: application/x-www-form-urlencoded;charset=utf-8

spring boot报Unsupported Media Type Content type '*/*;charset=UTF-8' not supported_第1张图片

2.服务端:

 controller

package com.example.demo;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class helloController {
    @RequestMapping(value="/hello")
    public String say(){
        String str="my spring boot web project hihihi";
        System.out.println(str);
        return str;
    }

    @RequestMapping(value="/test")
    public String testObj(@RequestBody User user){

        System.out.println(user.toString());
        return user.toString();
    }
}

实体user

package com.example.demo;

public class User {
    private String userName;
    private String pwd;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String toString(){
        return userName+""+pwd;
    }
}

3.输出

spring boot报Unsupported Media Type Content type '*/*;charset=UTF-8' not supported_第2张图片

 

转载于:https://www.cnblogs.com/pu20065226/p/10733787.html

你可能感兴趣的:(java,json,移动开发)