2017-01-30 209 views
0

这里是我的restful webservice,它有多个声明的方法。 当我声明两种方法,即GET和POST时,它会抛出servlet.init()异常。 这里是异常的根本原因:Restful web服务抛出servlet.init()异常

com.sun.jersey.api.container.ContainerException:在类rest.DatabaseOperations发现致命的问题。查看日志了解更多详情。

但是,当我声明只有post方法它完美的作品。

@Path("/databaseOperation") 
public class DatabaseOperations { 


@Path("/select") 
@GET 
@Produces(MediaType.APPLICATION_JSON) 
@Consumes(MediaType.APPLICATION_JSON) 
public String select(String data) { 
    } catch (Exception exception) { 
     exception.printStackTrace(); 
    } 
} 

@Path("/insert") 
@POST 
@Consumes(MediaType.APPLICATION_JSON) 
public void insert(String feed) { 
    } catch (Exception exception) { 
    } 
} 
} 
+0

你的'try {}'块在哪里? –

+0

尝试块在那里,我忘了把它放在这里。 –

回答

0

get方法不包含有效载荷,因此此注释是不正确

@Consumes(MediaType.APPLICATION_JSON) 

你可能需要类似的东西

@Path("/select") 
@GET 
@Produces(MediaType.APPLICATION_JSON) 
public String select(@QueryParam("data")String data) { 

注:我认为缺少try是排字错误

+0

但我必须通过网页上的angularJS发送数据。那么我怎么会在这里呢? –

+0

GET请求使用查询参数在URL yourserver.com/select?data=xxx中发送数据。如果您需要将JSON发送到您的服务器,请使用“POST”请求。你也许需要在服务器端解析JSON。使用bean更改'String'参数。请参阅http://stackoverflow.com/questions/7693669/how-to-post-a-json-object-to-a-jax-rs-service – pedrofb