0
votes

I have created a @RestController and mapped a @PostMapping request.

I am passing Content Type as "application/json" to the controller where I am reading the JSON with @RequestBody annotation mapping it to my Model class object.

@RestController:

@PostMapping("/saveUser")
public UserDetails save(@RequestBody UserDetails user) {
    System.out.println("from json input : "+user.idVal+" : "+user.getName());
    service.save(user);
    JSONObject jsn = new JSONObject();
    jsn.put("Result", user.getName());
    return user;
}

HTTP Request

{
"idVal":"asdadasd",
"name":"TestUser"
}

Model Class

import javax.persistence.Entity;
javax.persistence.Id;

@Entity
public class UserDetails {

    @Id
    public String idVal;
    public String name;

    public String getId() {
        return idVal;
    }
    public void setId(String id) {
        this.idVal = id;
    }

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

I am using Postman as the REST Client.

Following is the error I am getting:

HTTP Status 415 – Unsupported Media Type
Type Status Report

Description The origin server is refusing to service the request because the payload is in a format not supported by this method on the target resource.

Apache Tomcat/8.5.31

I have tried as following:

@PostMapping(value="/saveUser", consumes=MediaType.APPLICATION_JSON_VALUE, produces=MediaType.APPLICATION_JSON_VALUE)
public UserDetails save(@RequestBody UserDetails user) {
    System.out.println("from json input : "+user.idVal+" : "+user.getName());
    service.save(user);
    JSONObject jsn = new JSONObject();
    jsn.put("Result", user.getName());
    return user;
}

Edit:

Below is the complete Postman Request:

 POST /Zcrud/saveUser HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: fa4cfc76-d60c-f8dd-53b9-fd8b0b27ef7d

{
    "idVal":"asdadasd",
    "name":"TestUser"
}

Response:

<!doctype html><html lang="en"><head><title>HTTP Status 415 – Unsupported Media Type</title><style type="text/css">h1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} h2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} h3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} b {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} p {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;} a {color:black;} a.name {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 415 – Unsupported Media Type</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Description</b> The origin server is refusing to service the request because the payload is in a format not supported by this method on the target resource.</p><hr class="line" /><h3>Apache Tomcat/8.5.31</h3></body></html>
2
Do you have this dependency defined in your pom <dependencies> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.8.5</version> </dependency> </dependencies> Reference - Sadiq Ali
1) Why are you creating that useless JSONObject in your controller? 2) Please post the full HTTP request log. - chrylis -cautiouslyoptimistic-

2 Answers

0
votes

You are passing application/json in your request from Postman to Your Application. But your server doesn't understand what application/json is.

So, you have to define in your server too, what type of request is is gonna handle, and what type of response it is gonna produce.

to do so, change your @RestController's @PostMapping like below

@PostMapping(value ="/saveUser", consumes = MediaType.APPLICATION_JSON_VALUE,
        produces = MediaType.APPLICATION_JSON_VALUE)
public UserDetails save(@RequestBody UserDetails user) {
   System.out.println("from json input : "+user.idVal+" : "+user.getName());
   service.save(user);
   JSONObject jsn = new JSONObject();
   jsn.put("Result", user.getName());
   return user;
}

Hope it helps!! :)

0
votes

Add Content-Type: application/json;odata=verbose to your @PostMapping annotation