尝试构建Web服务时的ContainerException

时间:2014-04-29 06:01:33

标签: java web-services rest java-ee jersey

我收到ERROR表示root resource class中缺少Resource Config。这是我的网络服务代码:

public class MapErpService {

//first a URI for each resource,create using PUT
DBUtils db;
MapErpService()
{
    db=new DBUtils("zmaperp", "root", "root");
}

@PUT
@Path("/{objtype}/{objkey}")
@Consumes(MediaType.APPLICATION_JSON)
public Response createGeometry(@PathParam("objtype") String objtype,@PathParam("objkey") String objkey,JSONObject obj)
{
    //generate gisuniqkey here
    JSONObject geo=(JSONObject) obj.get("geometry");
    String gisuniqkey=(String) obj.get("gisuniqkey");
    String wkt=db.convertToWKT(geo.toJSONString());
    if(db.insertGeometryValues(gisuniqkey, objkey, objtype, wkt))
    {
        System.out.println("Data inserted successfully using PUT with objkey: "+objkey);
        return Response.status(201).entity("Data inserted successfully using PUT with objkey: "+objkey).build();
    }

    else
    {
        System.out.println("Data could not be inserted successfully");
        return Response.status(500).entity("Data inserted successfully using PUT with objkey: "+objkey).build();
    }
}

@POST
@Path("/{objtype}")
@Consumes(MediaType.APPLICATION_JSON)
public Response createGeometryUsingPost(@PathParam("objtype")String objtype,JSONObject obj)
{
    //both gisuniqkey and objkey have to be generated by magic here...
    JSONObject geo=(JSONObject) obj.get("geometry");
    String gisuniqkey=(String) obj.get("gisuniqkey");
    String objkey=(String)obj.get("objkey");
    String wkt=db.convertToWKT(geo.toJSONString());
    if(db.insertGeometryValues(gisuniqkey, objkey, objtype, wkt))
    {
        //set response code to 201(??) here
        System.out.println("Data inserted successfully using PUT with objkey: "+objkey);
        return Response.created(URI.create("http://localhost:8180/"+"GoogleMapsLoadingTest/"+objtype+"/"+objkey)).status(201).entity("Data inserted successfully using POST with objkey: "+objkey).build();
    }
    else
        //set response code 404??
        System.out.println("Data could not be inserted successfully");
    return Response.status(201).entity("Data could not be inserted successfully").build();
}

@GET
@Path("/{objtype}/{objkey}")
@Produces(MediaType.APPLICATION_JSON)
public JSONObject getGeometry(@PathParam("objtype")String objtype,@PathParam("objkey") String objkey)
{
    ResultSet rs=db.queryValue(objkey, objtype);
    JSONObject res=null;
    try {
        if(rs.next())   
        {
            String wkt=rs.getString("AsText(Geometry)");
            res=db.convertToGeoJSON2(wkt);
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return res;
}

@PUT
@Path("/{objtype}/{objkey}")
@Consumes(MediaType.APPLICATION_JSON)
public Response updateGeometry(@PathParam("objkey") String objkey,@PathParam("objtype") String objtype,JSONObject obj)
{
    String gisuniqkey=(String) obj.get("gisuniqkey");
    JSONObject geo=(JSONObject) obj.get("geometry");    
    String wkt=db.convertToWKT(geo.toJSONString());
    if(db.updateGeometryValue(objkey, objtype, wkt))
    {
        System.out.println("The update operation was successful for objkey: "+objkey);
        return Response.status(200).entity("The update operation was successful for objkey: "+objkey).build();
    }
    else
        //send 500 Server Error here
        System.out.println("The update operation failed completely");
    return Response.status(500).entity("The update operation failed completely").build();
}

@DELETE
@Path("/{objkey}/{objtype}")
public Response deleteGeometry(@PathParam("objkey") String objkey,@PathParam("objtype") String objtype){
    if(db.delete(objkey, objtype)){
        System.out.println("The delete operation was successful");
        return Response.status(200).entity("The delete operation was successful").build();
    }
    else
    {

        System.out.println("The delete operation failed miserably");
        return Response.status(500).entity("The delete operation failed miserably").build();
    }

}

//how do I do multiple...a basic attempt using the POST method
@POST
@Path("/{objtype}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public JSONObject getMultiGeometry(@PathParam("objtype") String objtype,JSONObject body){
    JSONObject res=null;
    JSONArray itab= (JSONArray) body.get("itab");
    ArrayList<String> keys=new ArrayList<String>();
    for(int i=0;i<itab.size();i++)
    {
        JSONObject keyObj=(JSONObject) itab.get(i);
        String objkey=(String) keyObj.get("objkey");
        keys.add(objkey);
        System.out.println("\nobjkey: "+objkey);
    }
    ResultSet rs=db.queryValues(keys);
    try {
        ArrayList<String> wktList=new ArrayList<String>();
        while(rs.next())
        {
            String wkt=rs.getString("AsText(GEOMETRY)");
            //String geo=db.convertToGeoJSON(wkt).toJSONString();
            //System.out.println("\nwkt: "+wkt+"\tgeoJSON: "+geo);
            wktList.add(wkt);
        }
        res=db.createGeometryCollection(wktList);
    }
    catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return res;
}
}

这是我的web.xml

    <display-name>GoogleMapLoadingTest</display-name>  
<servlet>
    <servlet-name>Jersey REST Services</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.hastha.maperp.webservice</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey REST Services</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

更新:

添加应用程序类没有任何效果:

  public class MapErpApplication extends Application {

public Set<Class<?>> getClasses()
{
    Set<Class<?>> res=new HashSet<Class<?>>();
    res.add(MapErpService.class);
    res.add(MapErpGetService.class);
    return res;
}
} 

我仍然收到此错误:

    com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.
com.sun.jersey.server.impl.application.RootResourceUriRules.<init>(RootResourceUriRules.java:99)
com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1359)
com.sun.jersey.server.impl.application.WebApplicationImpl.access$700(WebApplicationImpl.java:180)
com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:799)
com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:795)
com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)
com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:795)
com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:790)
com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:491)
com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:321)
com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:605)
com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:207)
com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:376)
com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:559)
javax.servlet.GenericServlet.init(GenericServlet.java:160)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
java.lang.Thread.run(Thread.java:744)

UPDATE2:

Now I get this error when performing a GET request from the browser

1 个答案:

答案 0 :(得分:0)

使用MapErpService(或其他字符串)为您的@Path("/erp")课程添加注释。