Anshu Kumar
Anshu Kumar

Reputation: 11

Unsupported Media type 415 @RequestBody Not working But @RequestParam is working

I have a Model class and a controller. I am posting json type data in the body of post man. But each time i'm getting an unsupported media type 415 error. Here is My Model class :

public class Model1 {
    private String name;
    private String password;
    public Model1() {
        System.out.println("In the Model");
    }
    public String getName() {
        return name;
    } 

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "Model1 [name=" + name + ", password=" + password + "]";
    }
}

And My Controller is :

@Controller
@ResponseBody
public class EcomController { 
    @RequestMapping(value="/getLogin", method=RequestMethod.POST, 
    consumes=MediaType.APPLICATION_JSON_VALUE)
    public String getLoginStatus(@RequestBody Model1 name){
        return "successfully called Post"+name.toString();
    }
}

I have used HttpServletRequest in the place of @RequestBody and it worked. But Why its not woking when I am using @RequestBody?

This is the postman snapshot. Here is the image of request from postman

{
    "name": "anshsh",
    "password": "vbvfjh"
}

This is the Screen shot of headers used in the request

Upvotes: 0

Views: 1801

Answers (3)

Michael Wijaya
Michael Wijaya

Reputation: 1

You don't need to put @ResponseBody there

also some example would be

@RestController 
public class EcomController { 
    @PostMapping("/getLogin")
    public String getLoginStatus(@RequestBody Model1 name){
        return "successfully called Post"+name.toString();
    } 
}

If you keep getting error, I think the problem is the servlet didn't find your controller registered.. Can you give more information in detail like the log when you try to compile?

Upvotes: 0

Anshu Kumar
Anshu Kumar

Reputation: 11

I found the mistake in my configuration file. adding <mvc:annotation-driven /> solved the problem.

Explanation :
My program was running well but problem was on Mapping the String (Json) into java objects. So there was some issue in default Mapping classes. Although, jackson was present in class-path, It wasn't working.
<mvc:annotation-driven />
this tag would register the HandlerMapping and HandlerAdapter required to dispatch requests to your @Controllers. In addition, it also applies some defaults based on what is present in your classpath. Such as: Support for reading and writing JSON, if Jackson is on the classpath.

Upvotes: 1

Yasin
Yasin

Reputation: 2029

There are two areas to check for such issues.

  1. Add Content-type as application/json in the request (which you have already done)
  2. Make sure you have jackson (core and data-bind) libraries in the classpath

The point #2 is to make sure that the application is able to convert between JSON and Java types.

To narrow down the issue further, check the logs and try GET API and see if the Java object is converted to JSON string.

Refer this link for complete working code.

Upvotes: 0

Related Questions