Error Spring MVC Rest Full

Asked

Viewed 68 times

0

Good night,

I’m trying to compile a code to run a simple Rest in spring mvc, but found with 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);
    }
}

Model class

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;
    }
}

POST JSON

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

Print of the Request inserir a descrição da imagem aqui

  • How the requisition is being made?

  • Through Postman, using the above json, with the content type application/json.

  • Okay, share a print of your request in Postman

  • I edited the Post @Leonardo Villela, I do not understand because the fields and types are correct.

1 answer

1


I believe it is related to the fact that your model does not have a public constructor,Serializable, performing these 2 steps, its class becomes a POJO(Plain Old Java Object), a pattern that Jackson (responsible for the serialization of objects) needs to be followed in order to achieve serialization without any customization Annotation.

Reference related to the subject POJO: https://en.wikipedia.org/wiki/Plain_old_Java_object

  • 1

    Thank you Leonardo, really was the lack of the 2 steps quoted in your reply, I am learning and I will study more about the Serializable class and also about the POJO.

  • Blz, I’m glad I could help you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.