gpt4 book ai didi

java - Jersey Jax-rs 出现容器异常

转载 作者:行者123 更新时间:2023-12-02 09:26:20 26 4
gpt4 key购买 nike

我是 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);

}

请帮帮我?

最佳答案

似乎问题出在 @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 会自动计算出来。 :-)

关于java - Jersey Jax-rs 出现容器异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12328456/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com