@FormParam、@QueryParam的区别

17 篇文章 0 订阅
12 篇文章 0 订阅

今天写了个功能简单的接口,但是一直在报错,先贴一下接口的实现方法:

  @GET
    @Path("/search/selLocation")
    @BaseInfo(desc = "直通车", status = app.dev)
    public String getLocation(@Context RequestContext rc, @Context HttpServletRequest request,
    						  @ParamDesc(isRequired = true, desc="keyWord") @FormParam("keyWord")String keyWord) {
		          return commonResourceService.getLocation(keyWord);
    	
    }

报错内容:

十一月 14, 2018 8:43:30 下午 com.sun.jersey.spi.inject.Errors processErrorMessages
严重: The following errors and warnings have been detected with resource and/or provider classes:
  SEVERE: A HTTP GET method, public java.lang.String com.lenovo.club.mall.common.CommonResource.getSelLocation(com.lenovo.commons.context.RequestContext,javax.servlet.http.HttpServletRequest,java.lang.String), should not consume any form parameter.
  WARNING: A sub-resource method, public java.lang.String com.lenovo.club.common.resource.EmotionResource.list(com.lenovo.commons.context.RequestContext,javax.servlet.http.HttpServletRequest), with URI template, "", is treated as a resource method
  WARNING: A sub-resource method, public java.lang.String com.lenovo.club.common.resource.MobileAskResource.mobileAsk(com.lenovo.commons.context.RequestContext,java.lang.String,java.lang.String), with URI template, "", is treated as a resource method
  WARNING: A HTTP GET method, public void com.lenovo.club.common.resource.VersionController.download(com.lenovo.commons.context.RequestContext,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,java.lang.String,java.lang.String), MUST return a non-void type.
  WARNING: A HTTP GET method, public void com.lenovo.club.proxy.resource.ClientProxyResource.transfer(com.lenovo.commons.context.RequestContext,javax.servlet.http.HttpServletResponse,java.lang.String), MUST return a non-void type.
  WARNING: A sub-resource method, public java.lang.String com.lenovo.club.lottery.resource.LotteryResources.lottery(com.lenovo.commons.context.RequestContext,javax.servlet.http.HttpServletRequest,int), with URI template, "", is treated as a resource method
十一月 14, 2018 8:43:30 下午 com.lenovo.swarm.servlet.MatrixServlet initiate
严重: Exception occurred when intialization
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:765)
	at com.lenovo.swarm.servlet.MatrixServlet.initiate(MatrixServlet.java:334)
	at com.lenovo.swarm.servlet.MatrixServlet$InternalWebComponent.initiate(MatrixServlet.java:89)
	at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:609)
	at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210)
	at com.lenovo.swarm.servlet.MatrixServlet.init(MatrixServlet.java:111)
	at com.lenovo.swarm.servlet.MatrixServlet.init(MatrixServlet.java:188)
	at javax.servlet.GenericServlet.init(GenericServlet.java:158)
	at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1144)
	at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1091)
	at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:773)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:133)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Unknown Source)

查看了很多网上的资源,都在说缺少相应的jar包,也按照要求在pom文件中添加了依赖,一直没有解决,最后请教了我们组的大佬,一眼就看出错来,原来是因为注解不对应的问题
在接口中,我要求的请求类型是GET,那么在参数注解中就不能使用@FormParam,应该使用@QueryParam
在这里插入图片描述
正确的代码:

  @GET
    @Path("/search/selLocation")
    @BaseInfo(desc = "直通车", status = app.dev)
    public String getLocation(@Context RequestContext rc, @Context HttpServletRequest request,
    						  @ParamDesc(isRequired = true, desc="keyWord") @QueryParam("keyWord")String keyWord) {
		          return commonResourceService.getLocation(keyWord);
    	
    }

下面说一下这个注解的用法:

@FormParam

@FormParam可以用来注入表单的参数为REST风格的Web服务
(图+下面的代码摘自网络https://www.cnblogs.com/chen-lhx/p/5599806.html)
在这里插入图片描述

<form method="POST" action="login">
    Email Address: <input type="text" name="email"><br>
    Password:      <input type="text" name="password">
    <input type="submit">
</form>
@Path("/")
public class LoginService
{
  @POST

  @Path("login")
  public String login(@FormParam("email") String e, @FormParam("password") String p) { 
    return "Logged with " + e + " " + p; 
  }
}

@QueryParam

@QueryParam 标注绑定一个路径段资源的方法参数值,例如,下面的方法将拦截HTTP GET http://server:port/login?zip=12345 和将查询参数 “zip” 注入到方法参数 “zip”

@Path("/")
public class LoginService
{
 @GET
 @Path("login/{zip}")
  public String login(@QueryParam("zip") String zip) {
     return "Id is " +id;
  }
}

@QueryParam可以方便快捷使用@DefaultValue标注,以便在没有查询参数时来避免空指针异常

 @GET
 @Path("login/{zip}")
 public String login(@DefaultValue("11111") @QueryParam("zip") String zip) {
     return "Id is " +id;
 }
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值