Http获取Web服务请求不起作用

时间:2014-04-30 05:25:43

标签: java json rest jersey jersey-1.0

我向此get发送url个请求:http://localhost:8180/GoogleMapsErp/EQUI/000001 我得到了error

 SEVERE: Servlet /GoogleMapsLoadingTest threw load() exception
  com.sun.jersey.spi.inject.Errors$ErrorMessagesException
at com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
at com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
at   com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:795)
at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:790)
at com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:491)
at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:321)
at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:605)
at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:207)
at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:376)
at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:559)
at javax.servlet.GenericServlet.init(GenericServlet.java:160)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1274)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1186)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1081)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5033)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5320)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)

这是我班上的相应方法:

@Path("/GoogleMapsErp")
public class MapErpService {

    @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);
            System.out.println(res);
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return res;
}

我可以使用resource获取servlet,但此处会引发404错误。我还创建了一个方法,其PUT个请求同一个uri。< / p>

我尝试将mimepulljersey-multipart添加到项目的库中,但错误仍然存​​在:

 asm-3.1.jar               jersey-servlet-1.18.jar
 jersey-client-1.18.jar    json-simple-1.1.1.jar
 jersey-core-1.18.jar      jsr311-api-1.1.1.jar
 jersey-json-1.18.jar      mimepull-1.6.jar
 jersey-multipart-1.8.jar  mysql-connector-java-5.1.30-bin.jar
 jersey-server-1.18.jar

的web.xml

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

这些是我在服务器启动时遇到的一些错误:

 com.sun.jersey.api.core.PackagesResourceConfig init
 INFO: Scanning for root resource and provider classes in the packages:
 com.hastha.maperp.webservice
 com.sun.jersey.api.core.ScanningResourceConfig logClasses
 INFO: Root resource classes found:
 class com.hastha.maperp.webservice.MapErpService
 com.sun.jersey.api.core.ScanningResourceConfig init
 INFO: No provider classes found.
 com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
 INFO: Initiating Jersey application, version 'Jersey: 1.18 11/22/2013 01:21 AM'
 com.sun.jersey.spi.inject.Errors processErrorMessages
 SEVERE: The following errors and warnings have been detected with resource and/or  provider classes:
 SEVERE: Consuming media type conflict. The resource methods public   
 javax.ws.rs.core.Response    com.hastha.maperp.webservice.MapErpService.updateGeometry(java.lang.String,java.lang.String,org.json.simple.JSONObject) and public javax.ws.rs.core.Response com.hastha.maperp.webservice.MapErpService.createGeometry(java.lang.String,java.lang.String,org.json.simple.JSONObject) can consume the same media type
 org.apache.catalina.core.ApplicationContext log
 SEVERE: StandardWrapper.Throwable

更新:

我对此感到困惑,I have posted the entire web-service code here

1 个答案:

答案 0 :(得分:3)

web.xml中,您可以创建以下Jersey servlet类路径设置

<servlet>
    <servlet-name>Jersey</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>your rest service.package.path</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>Jersey</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

然后你必须像

那样调用REST服务
http://localhost:8180/rest/GoogleMapsErp/EQUI/000001

另外看看这个美丽的东西

<强> Create RESTful web service in java(JAX-RS) using jersey

REST URL的语法如下所示enter image description here