Error Spring MVC Rest Full

0

Good evening,

I'm trying to compile a code to run a simple rest in spring mvc, but I encountered the error "The request sent by the client was syntactically incorrect."

Class Controller

package br.com.aprendendorest.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import br.com.aprendendorest.models.cliente;
import br.com.aprendendorest.repository.clienteRepository;

@RestController
public class clienteController {

    @Autowired
    clienteRepository clienteService;

    @GetMapping("/cliente")
    public ResponseEntity<List<cliente>> listCliente() {
        System.out.println("Entro Get");
        List<cliente> clientes = clienteService.listAll();
        return new ResponseEntity<List<cliente>>(clientes, HttpStatus.OK);
    }
    @PostMapping("/cliente")
    public ResponseEntity<cliente> creatCliente(@RequestBody cliente cli) {
        System.out.println("Entro no POST");
        System.out.println("ID: " + cli.getId() + ", NOME: " + cli.getNome() + ", EMAIL: " + cli.getEmail());
        clienteService.addCliente(cli);
        return new ResponseEntity<cliente>(cli, HttpStatus.CREATED);
    }
}

Class Model

package br.com.aprendendorest.models;

public class cliente {
    private Integer id;
    private String nome;
    private String email;

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public cliente(int id, String nome, String email) {
        this.id = id;
        this.nome = nome;
        this.email = email;
    }
}

JSON POST

{
    "id": 5,
    "nome": "Marcos",
    "email": "teste"
}

Request Print

    
asked by anonymous 07.03.2017 / 01:43

1 answer

1

I think it's related to the fact that your model does not have a public constructor, that it does not receive any arguments and is also not implementing the Serializable interface. By performing these 2 steps, your class becomes a Plain Old Java Object ), a standard that Jackson (responsible for object serialization) needs to be followed in order to perform the serialization without any annotation of customization.

Reference related to the subject POJO: link

    
07.03.2017 / 01:55