gpt4 book ai didi

java - CXF Restful 服务中的 HttpServletRequest.getParamter() 返回 null(Post)

转载 作者:行者123 更新时间:2023-12-02 03:34:42 24 4
gpt4 key购买 nike

我使用 Apache CXF 编写了一个 Web API。当我在 post 方法中使用 HttpServletRequest.getParamter() 时,它返回 null。代码如下:

@Path("/")
public class TokenService extends DigiwinBaseService {

private static void printRequest(HttpServletRequest httpRequest) {
System.out.println("\n\n Headers");
Enumeration headerNames = httpRequest.getHeaderNames();
while (headerNames.hasMoreElements()) {
String headerName = (String) headerNames.nextElement();
System.out.println(headerName + " = " + httpRequest.getHeader(headerName));
}
System.out.println("\n\n Parameters");
Enumeration params = httpRequest.getParameterNames();
while (params.hasMoreElements()) {
String paramName = (String) params.nextElement();
System.out.println(paramName + " = " + httpRequest.getParameter(paramName));
}
System.out.println("\n\n Row data");
System.out.println(extractPostRequestBody(httpRequest));
}

private static String extractPostRequestBody(HttpServletRequest request) {
if ("POST".equalsIgnoreCase(request.getMethod())) {
Scanner s = null;
try {
s = new Scanner(request.getInputStream(), "UTF-8").useDelimiter("\\A");
} catch (IOException e) {
e.printStackTrace();
}
return s.hasNext() ? s.next() : "null";
}
return "null";
}

@POST
@Consumes("application/x-www-form-urlencoded")
public Response Authorize(@FormParam("param") String param,
@FormParam("param2") String param2,@Context HttpServletRequest httpRequest) throws OAuthSystemException {
printRequest(httpRequest);
System.out.println("param:"+param);
System.out.println("param2:"+param2);
return Response.status(HttpServletResponse.SC_OK).entity("OK").build();
}
}

这是测试代码

   public class HttpClientTest {

public static void main(String[] args) throws Exception{

String url4 = "/api/services/Test";
String host = "127.0.0.1";
HttpClient httpClient = new HttpClient();
httpClient.getHostConfiguration().setHost(host, 8080, "http");
HttpMethod method = postMethod(url4);
httpClient.executeMethod(method);
String response = method.getResponseBodyAsString();
System.out.println(response);
}

private static HttpMethod postMethod(String url) throws IOException{
PostMethod post = new PostMethod(url);
post.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk");
NameValuePair[] param = {
new NameValuePair("param","param1"),
new NameValuePair("param2","param2"),} ;
post.setRequestBody(param);
post.releaseConnection();
return post;
}
}

这是打印输出:

Headers
content-type = application/x-www-form-urlencoded;charset=gbk
user-agent = Jakarta Commons-HttpClient/3.1
host = 127.0.0.1:8080
content-length = 26

Parameters

Row data
null

param:param1
param2:param2

为什么参数为空?我如何使用 HttpServletRequest.getParamter() 获取帖子参数

最佳答案

CXF 正在使用 POST 数据来填充 FormParams。

https://issues.apache.org/jira/browse/CXF-2993

解决方案是“无法修复”。在该问题中,他们建议使用 MultivaluedMap 来恢复所有参数,或仅使用 HttpServletRequest

选项1

@POST
@Consumes("application/x-www-form-urlencoded")
public Response Authorize( MultivaluedMap<String, String> parameterMap, @Context HttpServletRequest httpRequest) throws OAuthSystemException {
//parameterMap has your POST parameters

选项2

@POST
@Consumes("application/x-www-form-urlencoded")
public Response Authorize( @Context HttpServletRequest httpRequest) throws OAuthSystemException {
//httpRequest.getParameterMap() has your POST parameters

关于java - CXF Restful 服务中的 HttpServletRequest.getParamter() 返回 null(Post),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37582271/

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