3

我是 Jax-Rs 的新手。我尝试将数据从 html 发布到 Webresource 方法。在运行时我遇到异常,即

exception 

javax.servlet.ServletException: com.sun.jersey.api.container.ContainerException: Exception injecting parameters to Web resource method
    com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:310)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:314)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:239)


root cause 

com.sun.jersey.api.container.ContainerException: Exception injecting parameters to Web resource method
    com.sun.jersey.server.impl.model.method.dispatch.FormDispatchProvider$FormParamInInvoker.getParams(FormDispatchProvider.java:103)


root cause 


java.lang.ClassCastException: java.lang.String
    com.sun.jersey.core.util.MultivaluedMapImpl.getFirst(MultivaluedMapImpl.java:81)
    com.sun.jersey.core.util.MultivaluedMapImpl.getFirst(MultivaluedMapImpl.java:53)
    com.sun.jersey.server.impl.model.parameter.multivalued.StringExtractor.extract(StringExtractor.java:61)
    com.sun.jersey.server.impl.model.method.dispatch.FormDispatchProvider$FormParamInjectable.getValue(FormDispatchProvider.java:233)

我提到了下面的 Html 页面和 Web 资源方法:-

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Test Jax-RS Object</title>
</head>
<body>
<form action="services/fruitstore/loadObject1" method="POST" >
<table>
<tr>
    <td>ID:</td>
    <td><input type="text" name="id"></td>
</tr>
<tr>
    <td>Name:</td>
    <td><input type="text" name="name"></td>
</tr>
<tr>
    <td><input type="submit" Value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>

网络资源方法:-

@POST
@Path("loadObject1")
@Consumes("application/x-www-form-urlencoded")

public void loadObject1(@FormParam("id") String id,@FormParam("name") String name){
    System.out.println("====================");
    System.out.println("Fruit ID"+id+" Name"+name);

}

请帮我?

4

1 回答 1

0

似乎问题出在@Path- 你应该注释你的类,而不是方法。以下应该有效:

package net.asteasolutions.imagenation.boundary;

import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;

@Path("loadObject1")
public class TestService {

    @POST
    public void loadObject1(
        @FormParam("id") String id,
        @FormParam("name") String name
    ) {
        System.out.println("====================");
        System.out.println("Fruit ID"+id+" Name"+name);
    }
}

请注意,您不需要明确使用“application/x-www-form-urlencoded”,Jersey 会自动计算出来。:-)

于 2012-09-17T16:42:53.603 回答