I understand that the post method is getting it wrong at Postman

Asked

Viewed 100 times

1

My controller

package br.com.clinicamedica.controller.especialidade;

import static org.springframework.http.HttpStatus.CONFLICT;
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

import java.util.ArrayList;
import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import br.com.clinicamedica.core.EspecialidadesException;
import br.com.clinicamedica.core.especialidades.EspecialidadesConsultaService;
import br.com.clinicamedica.core.especialidades.EspecialidadesService;
import br.com.clinicamedica.core.especialidades.resource.EspecialidadeListaResource;
import br.com.clinicamedica.core.especialidades.resource.EspecialidadesResource;
import br.com.clinicamedica.utilitario.mensagem.Mensagem;
import lombok.extern.apachecommons.CommonsLog;

@CommonsLog
@RestController
@RequestMapping("/especialidades")
public class EspecialidadeController {

    @Autowired
    private EspecialidadesService especialidadesService;
    @Autowired
    private EspecialidadesConsultaService especialidadesConsultaService;
    private Collection<Mensagem> mensagens;

    @PostMapping(value = "/incluir", produces = APPLICATION_JSON_VALUE)
    public ResponseEntity<?> incluir(EspecialidadesResource resource) {
        mensagens = new ArrayList<>();
        try {
            EspecialidadesResource retorno = especialidadesService.incluir(resource, mensagens);
            return new ResponseEntity<>(retorno, OK);
        } catch (EspecialidadesException e) {
            Mensagem m = e.getMensagens().iterator().next();
            log.error(m.toString(), e);
            return new ResponseEntity<>(e.getMensagens().iterator().next(), CONFLICT);
        }
    }
}

In the Postman, when I do so, it does not work, because it is a post method, should not pass the object ? inserir a descrição da imagem aqui

But enough is empty inserir a descrição da imagem aqui

In the Postman, when I do so, it works, this example would not be of a method get ? inserir a descrição da imagem aqui

The methods GET are functioning normally.

  @GetMapping(value = "/buscarPeloId", produces = APPLICATION_JSON_VALUE)
  public ResponseEntity<?> buscarPeloId(Long id) {
      EspecialidadesResource retorno = especialidadesConsultaService.buscarPeloId(id);
      return new ResponseEntity<>(retorno, OK);
  }

What can it be ?

1 answer

1


Probably what is missing for you is to indicate your POST Header:

  1. Next to the tab Body, select Headers.
  2. In Key, write Content-Type.
  3. In Value, write application/json.
  4. Send us your post.

Como deve estar no Postman

The Content-Type shall be indicated as application/json so that the backend can correctly interpret the request. As MDN documentation:

The Content-Type header is used to indicate the file type of the recourse.

In requests, such as POST or PUT, the client tells the server what type given that it is, in fact, being sent.

  • Thanks that’s right It’s been adjusted and it’s working.

  • @Guilhermecostalopes considering that this answer is a solution to your question, please mark it as an answer by clicking on the "check" below the positive/negative votes. This helps other people to know that the problem has been solved and not waste time trying to solve it again or to try to use the solution if they have a similar problem. I noticed that you did not do this in your other questions as well, I advise to do it in case someone has given the solution :)

  • Done and thanks for your help,

Browser other questions tagged

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