在方法中同时读取路径参数和json正文请求

时间:2013-12-01 05:44:00

标签: java json web-services jersey

当我的方法看起来像这样时,我收到以下错误

@POST
    @Path("/share")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Response shareit(@PathParam("USERID") String userID,Map<String,String[]> paramValue){

.....
.....

}

如果从参数@PathParam(“USERID”)String userID或Map paramValue中删除任何一个,它都可以正常工作。我可以不读取路径参数并同时读取json正文请求吗?

ERROR:

Dec 01, 2013 11:30:20 PM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet /CloudServices threw load() exception
org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
[[FATAL] No injection source found for a parameter of type public javax.ws.rs.core.Response com.app.cloud.share.Share.shareit(java.lang.String,java.util.Map) at index 0.; source='ResourceMethod{httpMethod=POST, consumedTypes=[application/json], producedTypes=[application/json], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class com.rjil.cloud.share.Share, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@54fe88f3]}, handlingMethod=public javax.ws.rs.core.Response com.rjil.cloud.share.Share.shareit(java.lang.String,java.util.Map), parameters=[Parameter [type=class java.lang.String, source=USERID, defaultValue=null], Parameter [type=interface java.util.Map, source=null, defaultValue=null]], responseType=class javax.ws.rs.core.Response}, nameBindings=[]}']
    at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:427)
    at org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:162)
    at org.glassfish.jersey.server.ApplicationHandler$3.run(ApplicationHandler.java:287)
    at org.glassfish.jersey.internal.Errors$2.call(Errors.java:289)
    at org.glassfish.jersey.internal.Errors$2.call(Errors.java:286)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:286)
    at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:284)
    at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:302)
    at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:167)
    at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:349)
    at javax.servlet.GenericServlet.init(GenericServlet.java:160)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3954)
    at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:426)
    at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1345)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1530)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1519)
    at java.lang.Thread.run(Thread.java:744)

2 个答案:

答案 0 :(得分:1)

您似乎没有在任何{USERID}注释中定义@Path占位符(它不在您的资源方法上,我猜它不在资源类的@Path注释中)。您需要将@PathParam("USERID")@Path中的模板匹配:

@Path("/share/{USERID}")

答案 1 :(得分:0)

问题似乎与API方法的“返回类型”有关,您已定义为Response,请参阅错误消息:

  

找不到类型为public javax.ws.rs.core.Response的参数的注入源

您应该指定自己的“响应”类型(应该返回JSON对象)。要定义“请求”和“响应”类型,您可以使用JAXB(https://jaxb.java.net/),它基本上允许您指定一个对象,该对象定义要交换(接收和发送)的消息的“格式”。 API方法。这非常好,因为它允许您自动处理JSON(以及XML)和其他MIME表示。