kaushik
kaushik

Reputation: 556

Request Body for Post Method in Spring Controller

I need to implement a post endpoint which accepts a object(json). But some of the parameters in the object are optional. So when i try to make api call It is not mapping to that respective method.

It was showing Page not found Err : 404

Request body which i am sending contains only two fields, as the rest are optional.

Pojo for the object

public class Post {
  private String owner;

  private String activity;

  private Boolean edited;

  private String clientApp;

  private String serviceProvider;

  private long id;

  private Text text;

  public static class Text {

    private String text;

    public String getText() {
      return text;
    }

    public void setText(String text) {
      this.text = text;
    }
  }

  public String getOwner() {
    return owner;
  }

  public void setOwner(String owner) {
    this.owner = owner;
  }

  public String getActivity() {
    return activity;
  }

  public void setActivity(String activity) {
    this.activity = activity;
  }

  public Boolean getEdited() {
    return edited;
  }

  public void setEdited(Boolean edited) {
    this.edited = edited;
  }

  public String getClientApp() {
    return clientApp;
  }

  public void setClientApp(String clientApp) {
    this.clientApp = clientApp;
  }

  public String getServiceProvider() {
    return serviceProvider;
  }

  public void setServiceProvider(String serviceProvider) {
    this.serviceProvider = serviceProvider;
  }

  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }

  public Text getText() {
    return text;
  }

  public void setText(Text text) {
    this.text = text;
  }

}

Spring controller:

@RequestMapping(method = RequestMethod.POST, value="/publish/post",consumes = MediaType.APPLICATION_JSON_VALUE)
    public String publish(
      @RequestBody Post map) {
      logger.info("Post method")
      return "Completed";
    }

Curl Command :

curl -X POST \
  http://localhost:4569/rest/publish/post \
  -H 'Content-Type: application/json' \  
  -d '{
    "owner": "Owner of the Company",
    "text": {
        "text": "Png new image"
    }
}'

Payload

{
"owner": "Owner of the Company",
"text": {
    "text": "Png new image"
}

}

Rest Controller :

@RestController
@RequestMapping("/rest")
public class SocialController {

    private static final Logger logger = LoggerFactory.getLogger(SocialController.class);

    @RequestMapping(method = RequestMethod.POST, value="/publish/post",consumes = MediaType.APPLICATION_JSON_VALUE)
    public String publish(
      @RequestBody Post map) {
      logger.info("Post method")
      return "Completed";
    }
}

Upvotes: 0

Views: 2622

Answers (1)

Nitin Jangid
Nitin Jangid

Reputation: 164

You are missing application root in your URL http://localhost:4569/[App-name]/rest/publish/post

optional fields in pojo do not make problem to call rest methods.

Upvotes: 1

Related Questions