415-Spring中不受支持的媒体类型

时间:2018-11-15 09:24:40

标签: spring rest spring-mvc model-view-controller

我收到不受支持的媒体类型错误。 我的用户个人资料类如下所示

Class UserProfile{
private int age;
private String name,
private String currenecy;
}

这是控制器中的方法

@RequestMapping(value = "/Create", method=RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE, produces=MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<UserProfileResponse> createUserProfile(@RequestBody UserProfile userProfile)
{
UserProfileResponse userProfileResponse = new UserProfileResponse();
int id = createUserProfileData(userProfile)
userProfileResponse.setId(id);
return new ResponseEntity<UserProfileResponse>(userProfileResponse,HTTPStatus.OK);
}

我正在尝试通过POSTMAN发送请求,但是 错误415-不支持的媒体类型 我在POstman中的请求如下所示

Content-Type:application/json
Accept:application/json 
Method is : POST
{
"age":28,
"name":"Sam",
"currency": "INR"
}

enter image description here

建议我缺少什么?

3 个答案:

答案 0 :(得分:0)

别忘了选择“ JSON”格式,在文本区域中填写任意JSON字符串。

也一次使用“接受”或“内容类型”。

如果这不起作用,那么您可以通过删除消耗并手动添加标头来进行如下检查:

@RequestMapping(value = "/Create", method=RequestMethod.POST, headers = "Accept=application/json",produces=MediaType.APPLICATION_JSON_VALUE)

答案 1 :(得分:0)

我可以看到响应随您的代码一起返回。我故意返回同一对象只是为了测试连接性。以下是我的代码:

@RequestMapping(value = "/create", method= RequestMethod.POST,consumes= MediaType.APPLICATION_JSON_VALUE, produces=MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<UserProfile> createUserProfile(@RequestBody UserProfile userProfile)
{
    System.out.println("Got request");
    return new ResponseEntity<>(userProfile, HttpStatus.OK);
}

在UserProfile中使用了吸气剂和吸气剂

public class UserProfile {
    private int age;
    private String name;
    private String currenecy;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCurrenecy() {
        return currenecy;
    }

    public void setCurrenecy(String currenecy) {
        this.currenecy = currenecy;
    }
}

答案 2 :(得分:0)

最后,在花了一些时间之后。我弄清楚了为什么它不起作用。 在基于Java的Spring配置文件中,我错过了“ @EnableWebMvc”。 添加后,我的问题得到解决。

@Configuration
**@EnableWebMvc**   // This annotation was missing.
@ComponentScan(basePackages="com.hemant.*")
public class TestConfiguration  {}