Manny Marquez
Manny Marquez

Reputation: 29

Postman Post request for Spring boot saves the entire JSON body instead of just the field value. How do I fix this?

POSTMAN

My header 'Content-Type' is 'application/json' and I send it raw with JSON, which looks like this...

{
    "category": "House Maintenance"
}

Application

Category.java (model)

@Data
@Entity
@Table(name = "Categories")
public class Category implements Serializable{
    private static final long serialVersionUID = -8577891700634111561L;

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Long id;

    @Column(name = "category", nullable = false)
    private String category;

    public Category(String category){
        super();
        this.category = category;
    }

    public Category(){

    }
}

CategoryServiceImpl.java (save method)

@Override
  public void save(Category newCategory) {
      categoryRepository.save(newCategory);
  }

CategoryController.java (request)

@RequestMapping(value = "/categories", method = RequestMethod.POST)
    public void addCategory(@RequestBody String categoryName){
        Category newCategory = new Category(categoryName);
        categoryService.save(newCategory);
    }

Upvotes: 1

Views: 5750

Answers (2)

Manny Marquez
Manny Marquez

Reputation: 29

Answer

Thanks to Ralf Wagner, I went back and changed my method to the following.

//Old
method(@RequestBody String category)

//New
method(@RequestBody Category category)

I added '@JsonProperty' to my 'Category' model to avoid creating an additional class.

//Old
@Column(name = "category", nullable = false)
private String category;

//New
@Column(name = "category", nullable = false)
@JsonProperty(value = "category")
private String category;

My database now stores correctly.

Upvotes: 1

Ralf Wagner
Ralf Wagner

Reputation: 1517

You're converting the request to a String in your controller method, so you receive the whole body of the request and set the category field to the whole json string.

What you want to do is Spring Boot already giving you the converted dto:

@RequestMapping(value = "/categories", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public void addCategory(@RequestBody Category category){
  categoryService.save(category);
}

If you don't want to couple your entity to the request, you need to introduce an additional class e.g. CreateCategoryDto:

@Data
public class CreateCategoryDTO{

  @JsonProperty(value = "category") 
  private String category;
}

(I'm assuming you're using Lombok and Jackson. The annotation @JsonProperty(value = "category") is only added for illustrating how to decouple Java field names from the Json names.)

The controller then becomes

@RequestMapping(value = "/categories", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public void addCategory(@RequestBody CreateCategoryDto categoryDto){

  Category newCategory = new Category(categoryDto.getCategory());
  categoryService.save(newCategory);
}

Upvotes: 3

Related Questions