简体   繁体   English

在Spring MVC上执行POST请求时,HTTP状态415 –不支持的媒体类型

[英]HTTP Status 415 – Unsupported Media Type when doing POST request on Spring MVC

I am trying to send a post request on a simple Spring MVC web app and use RequestBody in my controller to convert the JSON into a Java Object but for whatever reason, I keep getting HTTP Status 415 – Unsupported Media Type . 我试图在简单的Spring MVC Web应用程序上发送发布请求,并在控制器中使用RequestBody将JSON转换为Java对象,但是由于任何原因,我一直收到HTTP Status 415 – Unsupported Media Type I have spent a lot of time trying to find a solution to this but nothing seems to be working. 我花了很多时间试图找到解决方案,但是似乎没有任何效果。

The get method in my Controller seems to be working fine. 我的控制器中的get方法似乎工作正常。 This is my original code 这是我的原始代码

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

@Controller
@RequestMapping(value = "/users", method = RequestMethod.POST)
public class MyControllerAgain {

    @RequestMapping(method = RequestMethod.POST, consumes = "application/json")
    public void handleJson(@RequestBody Contact c) {
        System.out.println(c);
    }

    @RequestMapping(method = RequestMethod.GET, consumes = "application/json")
    public void handleGet() {
        System.out.println("a");
    }
}

This is my Contact 这是我的Contact

public class Contact {

    int id;

    public String name;

    public int number;

    public Contact(){}

    // Getters and setters
}

I am sending a request with Postman and this is what it looks like 我正在与邮递员发送请求,这看起来像

POST /users HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Accept: application/json
Cache-Control: no-cache
Postman-Token: 511c9e03-4178-380d-58f8-db24a3245b9e

{
    "id":1,
    "name":"something",
    "number":1
}

I have also tried including Jackson dependencies in my pom.xml . 我还尝试在pom.xml包括Jackson依赖项。 I have tried altering consume value in @RequestMapping annotation and I have tried all combinations of headers accept and Content type in my request. 我尝试更改@RequestMapping批注中的消费值,并且尝试了请求@RequestMapping头accept和Content type的所有组合。

Also, If I use @ModelAttribute instead of @RequestBody , then everything works fine except all the fields in Contact class are null. 另外,如果我使用@ModelAttribute而不是@RequestBody ,那么一切正常,除了Contact类中的所有字段均为空。

Here is the github link - https://github.com/Sanil2108/test_springmvc 这是github链接-https: //github.com/Sanil2108/test_springmvc

To me it looks like that the jpa anotations are messing up the json deserialization. 对我来说,jpa注释似乎弄乱了json反序列化。

The error returned from the spring server could be misleading. 从Spring服务器返回的错误可能会引起误解。 Try using plain object with setters and getters and see if this changes anything. 尝试使用带有setter和getter的普通对象,看看这是否有任何改变。 You should search for some exceptions in the logs. 您应该在日志中搜索一些例外。

Add a mapping to the handleGet method, for example: 将映射添加到handleGet方法,例如:

@RequestMapping(value = "/get", method = RequestMethod.GET, consumes = "application/json")
public void handleGet() {
    System.out.println("a");
}

--UPDATE-- --UPDATE--

Remove the consumes = "application/json" part from you GET call. 从GET调用中删除consumes = "application/json"部分。 It sees that both requests that listen to "/users" can consume json data, but one is a GET and the other one is a POST. 它看到两个侦听“ / users”的请求可以使用json数据,但是一个请求是GET,另一个请求是POST。

--2nd UPDATE-- This will definitely work. -第2次更新-这肯定可以。 Tested. 测试。

@RestController
@RequestMapping(value = "/users", method = RequestMethod.POST)
public class ContactController
{
    @RequestMapping(method = RequestMethod.POST, consumes = "application/json")
        public void handleJson(@RequestBody Contact c) 
        {
           System.out.println(c);
        }
}

RequestMapping annotation not only has the consumes but also produces . RequestMapping注释不仅具有consumes而且具有produces

But to avoid all these settings for HTTP REST, you can use RestController annotation and GetMapping , PostMapping etc.. 但是要避免针对HTTP REST的所有这些设置,可以使用RestController注释和GetMappingPostMapping等。

You can find an example in my github 你可以在我的github上找到一个例子

Tried everything but couldn't it to work. 尝试了所有内容,但无法正常工作。 Maybe I was making a silly mistake somewhere or there was something seriously wrong with my configuration. 也许我在某个地方犯了一个愚蠢的错误,或者我的配置存在严重错误。 Anyway, I tried to make it work with Spring boot and it worked fine. 无论如何,我试图使其与Spring boot一起使用,并且工作正常。 For anyone who is interested, Here is the github link - https://github.com/Sanil2108/spring_hibernate/tree/master/spring_boot1 对于任何有兴趣的人,这里是github链接-https: //github.com/Sanil2108/spring_hibernate/tree/master/spring_boot1

Also, thanks to everyone who tried to help! 另外,感谢所有尝试提供帮助的人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM