【问题标题】:POST method Error HTTP 405 (Method not allowed)POST 方法错误 HTTP 405(方法不允许)
【发布时间】:2017-10-16 14:34:40
【问题描述】:

我正在构建一个简单的 Web 应用程序并尝试使用 servlet 和 RESTful Web 服务将数据插入到数据库中,我收到此错误 HTTP 405 (Method not allowed)。你能给我一些建议吗?

此代码来自网络服务。

@POST
    @Path("/2")
    @Produces(MediaType.TEXT_PLAIN)
    public boolean addBooks(@FormParam("id") int id, @FormParam("isbn") 
String isbn, @FormParam("title") String title, @FormParam("author") String 
author, @FormParam("genre") String genre, @FormParam("availability") Boolean 
availability, @Context HttpServletResponse servletResponse)
            throws IOException {
      boolean result = DBManager.getInstance().addBook(id, isbn, title, author, genre, availability);
      return result;
    }

此代码来自 servlet。

WebTarget target = client.target(getBaseURI());
Boolean ok = target.path("rest").path("hello/2").request()
                .accept(MediaType.TEXT_PLAIN).get(Boolean.class);

【问题讨论】:

    标签: web-services rest jsp servlets post


    【解决方案1】:

    您正在向服务器发送 GET 请求而不是 POST。

    试试:

    MultivaluedMap<String, String> formParams = new MultivaluedHashMap();
    formParams.put("title", ...);
    formParams.put("isbn", ...);
    ...
    Boolean ok = target.path("rest").
                        path("hello/2").
                        request().
                        accept(MediaType.TEXT_PLAIN).
                        post(Entity.form(formParams), Boolean.class);
    

    【讨论】:

    • 我试过这个,但出现了这个错误:SyncInvoker 类型中的方法 post(Entity>) 不适用于参数 (Class)
    • 好吧,我们都忘记了表单参数不是吗?
    猜你喜欢
    • 2018-12-20
    • 1970-01-01
    • 2019-07-05
    • 2013-12-05
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    • 2013-10-10
    相关资源
    最近更新 更多