【问题标题】:Etat HTTP 415 - Unsupported Media Type : GET not reaching Jersey RESTEtat HTTP 415 - 不支持的媒体类型:GET 未到达 Jersey REST
【发布时间】:2016-05-13 11:58:39
【问题描述】:

我正在使用带有休眠功能的 Spring MVC。我有一个使用 Jersey 编写的 RESTful Web 服务,如下所示:

 @GET  
  @Path("/interets/{idCentre}/annonces") 
  @Produces(MediaType.APPLICATION_JSON)
  public List<Annonce> getAnnonceParCentre(@PathParam(value="idCentre") int idCentre){
      return metier.getAnnonceParCentre(idCentre);
   }

这是我的 Annonce 课程:

package ma.ensa.model;
import java.util.Date;
public class Annonce implements java.io.Serializable {

private Integer idAnnonce;
private String libelleAnnonce;
private double prixAnnonce;
private Date dateAnnonce;
private double positionX;
private double positionY;
private double positionZ;
private int idCentre;
private String description;
private int idVille;
private int idPersonne;
private CentreInteret centreInteret;

public Annonce() {
}

public Annonce(String libelleAnnonce, double prixAnnonce, Date dateAnnonce, double positionX, double positionY,
        double positionZ, int idCentre, String description, int idVille, int idPersonne, CentreInteret centreInteret) {
    this.libelleAnnonce = libelleAnnonce;
    this.prixAnnonce = prixAnnonce;
    this.dateAnnonce = dateAnnonce;
    this.positionX = positionX;
    this.positionY = positionY;
    this.positionZ = positionZ;
    this.idCentre = idCentre;
    this.description = description;
    this.idVille = idVille;
    this.idPersonne = idPersonne;
}

public Integer getIdAnnonce() {
    return this.idAnnonce;
}

public void setIdAnnonce(Integer idAnnonce) {
    this.idAnnonce = idAnnonce;
}

public String getLibelleAnnonce() {
    return this.libelleAnnonce;
}

public void setLibelleAnnonce(String libelleAnnonce) {
    this.libelleAnnonce = libelleAnnonce;
}

public double getPrixAnnonce() {
    return this.prixAnnonce;
}

public void setPrixAnnonce(double prixAnnonce) {
    this.prixAnnonce = prixAnnonce;
}

public Date getDateAnnonce() {
    return this.dateAnnonce;
}

public void setDateAnnonce(Date dateAnnonce) {
    this.dateAnnonce = dateAnnonce;
}

public double getPositionX() {
    return this.positionX;
}

public void setPositionX(double positionX) {
    this.positionX = positionX;
}

public double getPositionY() {
    return this.positionY;
}

public void setPositionY(double positionY) {
    this.positionY = positionY;
}

public double getPositionZ() {
    return this.positionZ;
}

public void setPositionZ(double positionZ) {
    this.positionZ = positionZ;
}

public int getIdCentre() {
    return this.idCentre;
}

public void setIdCentre(int idCentre) {
    this.idCentre = idCentre;
}

public String getDescription() {
    return this.description;
}

public void setDescription(String description) {
    this.description = description;
}

public int getIdVille() {
    return this.idVille;
}

public void setIdVille(int idVille) {
    this.idVille = idVille;
}

public int getIdPersonne() {
    return this.idPersonne;
}

public void setIdPersonne(int idPersonne) {
    this.idPersonne = idPersonne;
}
}
}

DaoAnnonce 类中的方法:

@Override
public List<Annonce> getAnnonceParCentre(int id) {
    Session session = sessionFactory.openSession();
    Query qr = session.createQuery("from Annonce where idCentre=:id").setInteger("id",id);
    @SuppressWarnings("unchecked")
    List<Annonce> crs = qr.list();
    session.close();
    return crs;
}

当我尝试执行 GET 时:http://localhost:8080/projet_stage_v1/rs/interets/2/annonces

根本原因:

    Grave: The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container
java.io.EOFException: No content to map to Object due to end of input
at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2766)
at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2682)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1308)
at org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:419)
at com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy.readFrom(JacksonProviderProxy.java:139)
at com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:488)
at com.sun.jersey.server.impl.model.method.dispatch.EntityParamDispatchProvider$EntityInjectable.getValue(EntityParamDispatchProvider.java:123)
at com.sun.jersey.server.impl.inject.InjectableValuesProvider.getInjectableValues(InjectableValuesProvider.java:46)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$EntityParamInInvoker.getParams(AbstractResourceMethodDispatchProvider.java:153)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:183)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1511)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1442)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1391)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1381)

【问题讨论】:

    标签: spring hibernate rest jersey


    【解决方案1】:

    您是否在 GET 请求中发送 Accept 标头?它的值应该是application/json

    【讨论】:

    • 是的。我添加了“内容类型:应用程序/json”但不起作用。当我尝试使用localhost:8080/projet_stage_v1/rs/annonces 例如没有参数时,我得到结果“OK”,但是当我尝试使用参数时,我得到了错误。我不明白为什么??
    • 接受不同于内容类型。尝试使用“接受:应用程序/json”。内容类型与 GET 请求无关,因为它们没有请求正文。接受是映射到您的“@Produces”注释的内容。内容类型映射到“@Consumes”注释。
    • 我收到另一个错误:严重:找不到 Java 类 int、Java 类型 int 和 MIME 媒体类型 application/octet-stream 的消息正文阅读器。注册的与MIME媒体类型兼容的消息体阅读器有:application/octet-stream
    猜你喜欢
    • 2015-08-21
    • 2015-04-23
    • 2017-07-05
    • 2015-08-29
    • 2016-06-06
    • 2013-01-16
    相关资源
    最近更新 更多