简体   繁体   中英

Getting Unsupported Media Type when trying to access POST Json consumer endpoint developed using Jersey

I am getting this error when I try to execute my POST webservice methods:

415错误

I am using Postman to send the request:

postman-post-json请求

My entity is below:

@Entity
@XmlRootElement
@Table(name = "bandeiras")
public class BandeiraCartao extends EntidadeBase {

  private static final long serialVersionUID = 5944226798248177637L;

  @XmlElement
  @Column(length = 60, nullable = false)
  private String nome;

  @XmlElement
  @Column(nullable = false)
  private Integer codigoImagem;

  //getters and setters//
}

This is the superclass:

@MappedSuperclass
public abstract class EntidadeBase implements Serializable {

private static final long serialVersionUID = -3912856473903279467L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@PodamStrategyValue(value = NullStrategy.class)
private Long id;

  @Column(name = "cadastro_data", nullable = false)
  @PodamStrategyValue(value = PastDateStrategy.class)
  private LocalDate dataCadastro;

  @Column(name = "modificado_data", nullable = false)
  @PodamStrategyValue(value = PastDateStrategy.class)
  private LocalDate dataModificacao;

  @Column(nullable = false)
  @PodamBooleanValue(boolValue = false)
  private Boolean modificado;

  @Column(nullable = false)
  @PodamBooleanValue(boolValue = true)
  private Boolean ativo;

  //getters and setters//
}

and finally there is the endpoint method I am calling:

@Path("/bandeira")
public class BandeiraCartaoResource extends AbstractResource<BandeiraCartao> {

  private static final long serialVersionUID = 7080737992715054407L;

  @POST
  @Override
  @Transactional
  public Response inserir(@NotNull BandeiraCartao entidade) {
      return super.inserir(entidade);
  }

  ...
  }

I added Genson dependency in order to dont have to provide a MessageBodyWritter implementation of my own. I also added MOXy jersey dependency trying to solve this issue, but it didnt work. As additional information, its running on tomcat 8. Any clue guys?

From what I just tested...

Simple using the JSON raw type in post man doesn't set the the required header. You can click the Preview button and it will show you a preview of the entire request, including headers. What you need is a header Content-Type:application/json . You will see it is not there in the preview window. You can set it though with the Headers button on the main screen. Just set the Header to Content-Type and the value to application/json .

UPDATE

If you open up the Chrome developer tools and make the request (select the "Network/XHR" tab), you can see the default headers that gets set with the XHR request. It's actually more than just the Cache-Control you see in the Postman preview window. You will see it explicitly sets the content type to text/plain

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM