表单数据以Java中的其余PUT方法 [英] Form data to rest PUT method in java

查看:65
本文介绍了表单数据以Java中的其余PUT方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java和REST API的初学者.我在将表单数据从HTML传递到rest PUT方法时遇到问题.当我用谷歌搜索时,大多数可用的解决方案都是针对POST方法的,建议使用FormParam.就我而言,它显示以下错误:

I am beginner for Java and REST API. I have a problem passing form data from HTML to rest PUT method. When I google about this, most solutions available are for POST method, that recommended to use FormParam. In my case, it shows below error:

请求行中接收到的方法对于源服务器是已知的,但目标资源不支持.

The method received in the request-line is known by the origin server but not supported by the target resource.

即使我使用PathParam,也会返回相同的错误:

Even I use PathParam, same error is returned:

请求行中接收到的方法对于源服务器是已知的,但目标资源不支持.

The method received in the request-line is known by the origin server but not supported by the target resource.

以及一些Spring Boot解决方案.但是我没有用.

And some solution for Spring Boot. But I did not use that.

PUT方法:

@PUT
@Path("/update")
@Produces(MediaType.TEXT_HTML)
public String updCard(@PathParam("cardNo") String cardNo,  
        @PathParam("reportId") int reportId
        ) throws SQLException { 

    Card c = new Card(cardNo, reportId); 

    System.out.println(cardNo + reportId);
    
    return "";
}

表格:

 <form method="PUT" action="rest/card/update">
  <label for = "cardNo">Card No: </label> <input type="text" name = "cardNo" id = "cardNo"><br/>
  <label for = "reportId">Report Id:</label> <input type="text" name = "reportId" id = "reportId"> <br/>
  <button type="submit">Update</button>  

那么,如何在Jersey的PUT方法中获取表单数据?

So, how do I get the form data in PUT method in Jersey?

推荐答案

许多人在使用HTML形式的PUT方法中提到,HTML标准当前不支持PUT.大多数框架将要做的是提供一种解决方法.泽西岛(Jersey)的

As mentioned by many in Using PUT method in HTML form, PUT is not currently supported by the HTML standard. What most frameworks will do is offer a workaround. Jersey has such a workaround with its HttpMethodOverrideFilter. What you must do is use a POST method and add a _method=put query parameter and the filter will switch the POST to a PUT.

您首先需要注册过滤器.如果您使用的是ResourceConfig,只需

You first need to register the filter. If you are using a ResourceConfig just do

@ApplicationPath("api")
public class JerseyConfig extends ResourceConfig {

    public JerseyConfig() {
        ...
        register(HttpMethodOverrideFilter.class);
    }
}

如果您使用的是web.xml,请这样做

If you are using a web.xml, then do

<init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>org.glassfish.jersey.server.filter.HttpMethodOverrideFilter</param-value>
</init-param>

然后在HTML中,只需将_method=put查询参数添加到URL.下面是我用来测试的示例

Then in your HTML, you will just add the _method=put query param to the URL. Below is an example I used to test

<form method="post" action="/api/form?_method=put">
    <label>
        Name:
        <input type="text" name="name"/>
    </label>
    <label>
        Age:
        <input type="number" name="age"/>
    </label>
    <br/>
    <input type="submit" value="Submit"/>
</form>

在您的资源方法中,您将使用@PUT@FormParam s作为参数

And in your resource method you will use @PUT and @FormParams for the paramters

@PUT
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response form(@FormParam("name") String name,
                     @FormParam("age") String age,
                     @Context UriInfo uriInfo) {

    URI redirectUri = UriBuilder
            .fromUri(getBaseUriWithoutApiRoot(uriInfo))
            .path("redirect.html")
            .queryParam("name", name)
            .queryParam("age", age)
            .build();
    return Response.temporaryRedirect(redirectUri).build();
}

private static URI getBaseUriWithoutApiRoot(UriInfo uriInfo) {
    String baseUri = uriInfo.getBaseUri().toASCIIString();
    baseUri = baseUri.endsWith("/")
            ? baseUri.substring(0, baseUri.length() - 1)
            : baseUri;
    return URI.create(baseUri.substring(0, baseUri.lastIndexOf("/")));
}

应该可以根据我的测试进行工作

It should work from what I tested

这篇关于表单数据以Java中的其余PUT方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆