在 PUT 中指定参数 - Rest Web 服务

发布于 2024-10-01 17:13:46 字数 1062 浏览 0 评论 0原文

我正在使用 Jersey API 构建 REST Web 服务,但在将参数传递给 PUT 方法时遇到问题。方法是这样的:

@PUT
@Consumes("text/html")
public void putHtml (String content) {
    System.out.println("Content"+content);
}

我使用以下方式调用它:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript">
            function passName()
            {

                xmlHttp=new XMLHttpRequest();
                var url = "/RestWebApp/resources/greeting";
                xmlHttp.open("PUT",url,false);
                xmlHttp.send();
            }
        </script>
        <title>REST Testing</title>
    </head>
    <body>
        Put name:<input type="text" name="name"/>
        <br />
        <input type="button" value="Put" onclick="passName()"/>
    </body>
</html>

PUT 方法被调用,因为我在服务器控制台(即 Glassfish)中打印了“Content”,但参数不是似乎正在被阅读。是否有 @statement 或者我应该添加到参数中的东西?

谢谢! Krt_马耳他

I'm building a REST web service using the Jersey API and I've having a problem passing the parameter to the PUT method. The method is this:

@PUT
@Consumes("text/html")
public void putHtml (String content) {
    System.out.println("Content"+content);
}

I'm calling it using the following:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript">
            function passName()
            {

                xmlHttp=new XMLHttpRequest();
                var url = "/RestWebApp/resources/greeting";
                xmlHttp.open("PUT",url,false);
                xmlHttp.send();
            }
        </script>
        <title>REST Testing</title>
    </head>
    <body>
        Put name:<input type="text" name="name"/>
        <br />
        <input type="button" value="Put" onclick="passName()"/>
    </body>
</html>

The PUT method is being called since I'm getting "Content " printed in the server console (which is Glassfish) but the parameter is not being read it seems. Is there an @statement or something which I should add to the parameter?

Thanks!
Krt_Malta

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山川志 2024-10-08 17:13:46

您没有在客户端代码中设置请求正文。

您需要发送一些内容,例如

xmlHttp.Send("here is my content")

但是您已声明接收端点接受“text/html”。我会更改它以接受“文本/纯文本”。或者,如果您确实想接收多个值,您可以接受“application/x-www-urlencoded-form”。但是您需要在 JavaScript 中自行格式化。 Web 浏览器不知道如何 PUT 表单。他们只能发布表单。

You are not setting the body of the request in your client code.

You need to send some content, e.g.

xmlHttp.Send("here is my content")

However you have declared your receiving endpoint to accept "text/html". I would change that to accept "text/plain" instead. Or if you really want to receive multiple values you could accept "application/x-www-urlencoded-form". However you will need to format that yourself in the javascript. Web browsers don't know how to PUT forms. They can only POST forms.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文