1

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

我使用的技术:

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

爪哇:

@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

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

4

1 回答 1

1

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

于 2016-07-06T16:40:38.530 回答