2016-07-06 87 views
0

我在Java EE 6中编写了一个RESTful服务应用程序。我在定义使用@FormParam注释的HTTP GET服务方法时遇到了困难。@FormParam注释上的Java RESTful服务错误

技术,我用:

  • 的Eclipse EE开普勒IDE
  • JDK V7
  • Glassfish的3(基于Java 6)
  • 泽西(Glassfish的部分)

Java:

@GET 
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) 
@Produces(MediaType.TEXT_HTML) 
@Path("/getFromForm") 
public String getFromForm(@FormParam("input") String input){ 
    log.log(Level.INFO, "Invoked getFromForm"); 
    String html = "<html> <body>" 
      +"<center><h1> "+input+"</h1></center>" 
      +"</body></html>"; 
    return html; 
} 

JSP:

<form action="/RESTfulServiceDrill/rest/v6/html/getFromForm" method="get" 

enctype="application/x-www-form-urlencoded"> 

<label> Add message: </label> 
<input type="text" name="input"/> 

<input type="submit" value="OK"/> 
<input type="reset" value="clear"/> 

</form> 

例外:

java.lang.IllegalStateException: The @FormParam is utilized when the content type of the request entity is not application/x-www-form-urlencoded 

任何想法的罪魁祸首是什么?

+0

非常好的一点;这是正确的答案 – ucas

回答

1

您应该使用POST,而不是GET。使用GET时,浏览器不会设置Content-Type标题,因为参数是在URL中发送的,而不是正文。如果你想保持它与GET,使用@QueryParam,它捕获URL中的查询字符串的参数