1

作成したJax-RSWebサービスに少し問題があります。タイプ「Beitrag」に適合するJson-Objectを投稿するたびに、次のエラーが発生します。

SCHWERWIEGEND: The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container
org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class javax.xml.bind.JAXBElement<de.dhbw.pgbackend.Beitrag>]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: org.apache.catalina.connector.CoyoteInputStream@2771f735; line: 1, column: 2] at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObjectUsingNonDefault(BeanDeserializer.java:740)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:683)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2695)
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$VoidOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:166)
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)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:298)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)
11.01.2013 21:12:41 org.apache.catalina.core.StandardWrapperValve invoke
SCHWERWIEGEND: Servlet.service() for servlet [Jersey REST Service] in context with path [/de.dhbw.pgbackend] threw exception [org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class javax.xml.bind.JAXBElement<de.dhbw.pgbackend.Beitrag>]: can not instantiate from JSON object (need to add/enable type information?)
 at [Source: org.apache.catalina.connector.CoyoteInputStream@2771f735; line: 1, column: 2]] with root cause
 org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class javax.xml.bind.JAXBElement<de.dhbw.pgbackend.Beitrag>]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: org.apache.catalina.connector.CoyoteInputStream@2771f735; line: 1, column: 2]
at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObjectUsingNonDefault(BeanDeserializer.java:740)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:683)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2695)
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$VoidOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:166)
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)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:298)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)

これは私のクラスです:

@XmlRootElement  
public class Beitrag {
public int id;
public String user_id;
public String typ;
public String kategorie;
public String titel;
public String beschreibung;
public String preis;
public String datum;
public String name;
public String telefon;
public String email;

public Beitrag(){

}

public Beitrag(int id, String user_id, String typ, String kategorie, String titel, 
                  String beschreibung, String preis, String datum, String name, String telefon, String email){
    this.setId(id);
    this.setUser_id(user_id);
    this.setTyp(typ);
    this.setKategorie(kategorie);
    this.setTitel(titel);
    this.setBeschreibung(beschreibung);
    this.setPreis(preis);
    this.setDatum(datum);
    this.setName(name);
    this.setTelefon(telefon);
    this.setEmail(email);
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getUser_id() {
    return user_id;
}

public void setUser_id(String user_id) {
    this.user_id = user_id;
}

public String getTyp() {
    return typ;
}

public void setTyp(String typ) {
    this.typ = typ;
}

public String getKategorie() {
    return kategorie;
}

public void setKategorie(String kategorie) {
    this.kategorie = kategorie;
}

public String getTitel() {
    return titel;
}

public void setTitel(String titel) {
    this.titel = titel;
}

public String getBeschreibung() {
    return beschreibung;
}

public void setBeschreibung(String beschreibung) {
    this.beschreibung = beschreibung;
}

public String getPreis() {
    return preis;
}

public void setPreis(String preis) {
    this.preis = preis;
}

public String getDatum() {
    return datum;
}

public void setDatum(String datum2) {
    this.datum = datum2;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getTelefon() {
    return telefon;
}

public void setTelefon(String telefon) {
    this.telefon = telefon;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

}

@Path("/putbeitrag")
public class PutBeitrag {

@Path("/newBeitrag")
@POST
@Consumes(MediaType.APPLICATION_JSON)
public void newBeitrag(JAXBElement<Beitrag> beitrag){
    Beitrag b = beitrag.getValue();
    Connection conn = MySQLConnection.getInstance();
    if(conn != null)
    {
        try{
        // Anfrage-Statement erzeugen.
        Statement query;
        query = conn.createStatement();

        // Beitrag hinzufügen

            String sql = "INSERT into Beitraege (kategorie, titel, beschreibung, preis, bild, datum, name, telefon, email) " +
                    "values ("+b.getUser_id()+",'"+b.getTyp()+"','"+b.getTitel()+"','"+b.getKategorie()+"','"+b.getTitel()+"','"+b.getBeschreibung()+"'" +
                            ",'"+b.getPreis()+"',null,'"+b.getDatum()+"','"+b.getName()+"','"+b.getTelefon()+"','"+b.getEmail()+"')";
            query.executeQuery(sql);

        }catch (SQLException e) {
            e.printStackTrace();
        }
   }
}

@Path("/updateBeitrag")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public String updateBeitrag(JAXBElement<Beitrag> beitrag){
    Beitrag b = beitrag.getValue();
    Connection conn = MySQLConnection.getInstance();
    if(conn != null)
    {
        try{
        // Anfrage-Statement erzeugen.
        Statement query;
        query = conn.createStatement();

        // Beitrag ändern

            String sql = "UPDATE beitraege SET typ='"+b.getTyp()+"', SET kategorie='"+b.getKategorie()+"', SET titel='"+b.getTitel()+"', SET beschreibung='"+b.getBeschreibung()+"', SET preis='"+b.getPreis()+"', SET datum='"+b.getDatum()+"', SET name='"+b.getName()+"'," +
                    " SET telefon='"+b.getTelefon()+"', SET email='"+b.getEmail()+"' WHERE id="+b.id;
            query.executeQuery(sql);

        }catch (SQLException e) {
            e.printStackTrace();
        }
   }
    return "ok";
}

@Path("/deleteBeitrag")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public String deleteBeitrag(JAXBElement<Beitrag> beitrag){
    Beitrag b = beitrag.getValue();
    Connection conn = MySQLConnection.getInstance();
    if(conn != null)
    {
        try{
        // Anfrage-Statement erzeugen.
        Statement query;
        query = conn.createStatement();

        // Beitrag löschen

            String sql = "DELETE FROM Beitraege Where id="+b.getId();
            query.executeQuery(sql);

        }catch (SQLException e) {
            e.printStackTrace();
        }
   }
    return "ok";
}
}

どうしたらいいのかわからない。あなたが私を助けてくれることを願っています。ありがとう

4

1 に答える 1

0

フル引数コンストラクターに@JsonCreatorアノテーションを付ける必要があります。

@JsonCreator
public Beitrag(int id, String user_id, String typ, String kategorie, String titel, 
              String beschreibung, String preis, String datum, String name, String telefon, String email){
...

リソース自体にはJAXBElementピースは必要ないため、次のようになります。

@Path("/newBeitrag")
@POST
@Consumes(MediaType.APPLICATION_JSON)
public void newBeitrag(Beitrag beitrag){

入力JSONも不正な形式である可能性があります。この後さらにエラーが発生した場合は、それがどのように見えるかを確認できるように投稿してください。

最後に、より標準的なREST形式の使用を検討する必要があります。リソースbeitragがある場合、それにアクセスする一般的な方法は次のとおりです。

  • GET/beitrag-すべてのアイテムを取得
  • PUT/beitrag-新しいアイテムを作成します
  • GET / beitrag/{id}-IDが{id}のアイテムを取得します
  • POST / beitrag/{id}-IDが{id}のアイテムを更新します
  • DELETE / beitrag/{id}-IDが{id}のアイテムを削除します

これにはさまざまなバリエーションがありますが、このようなものは、/ newBeitrag、/udateBeitragなどのパスを持つよりもはるかに明確です。

于 2013-01-12T12:35:25.010 に答える