Spring Rest Mapping to an Object

I just did a rookie mistake that costed me a few hours.

If you want Spring to be able to create an object from the json you are sending to a controller you need to make sure that you are not creating a constructor with parameters without a default constructor.

I simply removed my constructor since it was not used and everything started to work as expected.

The 400 Bad Request stopped puzzling me.

The POJO is simple:

package com.cinq.example.v1;

public class Request {

 private String version;
 private String template;
 private int lifespan;

 public int getLifespan() {
 return lifespan;

 public String getVersion() {
 return version;

 public String getTemplate(){
 return template;

 public void setVersion(final String version){
 this.version = version;

 public void setTemplate(final String template){
 this.template = template;

 public void setLifespan(final int lifespan){
 this.lifespan = lifespan;

The controller that takes the json to create the object is also very simple:

package com.cinq.example.v1;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

 * API for something
@RequestMapping(value = "/api/v1")
public class Management {

	private final static Logger LOGGER = LoggerFactory.getLogger(Management.class);

	@RequestMapping(value = "/something", consumes = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
	public Response creation(@RequestBody final Request request){
		LOGGER.info("Will create something with " + request.getVersion());
		try {
		} catch ( final InterruptedException exception ) {
			LOGGER.error("Could not sleep for 5 secs.");

		return(new Response("test001", "s1", "sp1", "u1", "up1", 100L));

The Response object is as simple as the Request one and has many String fields.

Lesson Learned

If you need to create a constructor make sure you have the default one defined as well.

Published by


Java developper that loves photography and an excellent espresso

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s