2016-05-13 95 views
0

我正在使用Spring MVC与休眠。我有一个RESTful Web服务,使用泽西写的,看起来像这样:Etat HTTP 415 - 不支持的媒体类型:GET没有达到Jersey REST

@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) 

回答

0

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

+0

是的。我添加“Content-Type:application/json”但不起作用。当我尝试与http:// localhost:8080/projet_stage_v1/rs/annonces例如没有参数我得到的结果“确定”,但当尝试使用参数我得到错误。我不明白为什么?? – spoonatte

+0

接受与Content-type不同。试试“Accept:application/json”。内容类型与GET请求无关,因为它们没有请求主体。接受是映射到您的“@Produces”注释的内容。内容类型被映射到“@Consumes”注释。 – Randy

+0

我收到另一个错误:** Grave:未找到Java类int,Java类型int和MIME媒体类型application/octet-stream的消息正文阅读器。 与MIME媒体类型兼容的注册邮件正文阅读器为: application/octet-stream ** – spoonatte

相关问题