当前位置: 首页 > 知识库问答 >
问题:

jhipster不使用POST REST API

别永年
2023-03-14

我想在JHIPSTER中向实体发布值

我的实体名称Hussain中有一个名为name的字段,其类型为Long

我写了这段代码,但它给我一个错误401,如果只张贴一个名字,如果我添加id与它,我得到400个错误


        private void sendPost(long n) throws Exception {
    String url = "http://localhost:8080/api/hussains";

            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setDoOutput(true);
            con.setDoInput(true);
            //add request header
            con.setRequestProperty("Content-Type", "application/json");
            con.setRequestProperty("Accept", "application/json");
            con.setRequestProperty("Authorization", "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImF1dGgiOiJST0xFX0FETUlOLFJPTEVfVVNFUiIsImV4cCI6MTUzNDA4Nzg4OX0.AbI_AmN8ePTZ2blULuAKlls-YUPYMD9EHBqIgk_fbktdzJH7hhkEYhQw7settlM04n5N2MHRtGzC1b4z_PDw-Q");

            // optional default is POST
            con.setRequestMethod("POST");

             //Create JSONObject here
            JSONObject jsonParam = new JSONObject();
            jsonParam.put("name", 10);
            OutputStreamWriter out = new   OutputStreamWriter(con.getOutputStream());
            out.write(jsonParam.toString());
            out.close();  



            BufferedReader in = new BufferedReader(
                    new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            //print result
            System.out.println(response.toString());

            int responseCode = con.getResponseCode();
            System.out.println("\nSending 'POST' request to URL : " + url);
            System.out.println("Response Code : " + responseCode);


        }

我有一个错误


    java.io.IOException: Server returned HTTP response code: 401 for URL: http://localhost:8080/api/hussains
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1840)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
        at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)

共有1个答案

林君博
2023-03-14

首先,您不必用POST方法发送id,因为id将由您的后端创建。

您得到的401是未经授权的,所以您必须查明您的请求是否经过身份验证和未经授权。

你得到400因为错误的请求。

 类似资料:
  • 我使用的例子来自https://github.com/mraible/jhipster5-demo.我在dev和pro中使用了Postgresql。已成功生成包含实体的项目。 当我创建新博客并保存时,我遇到了这个错误。 com中的异常。阿斯德夫。博客网状物Rest博客资源。createBlog()的cause='java。lang.IllegalStateException:org。冬眠Trans

  • 我正在使用Jhipster来生成API。 在Spring boot中还需要做什么来启用CORS吗?

  • 我想开发一个SaaS应用程序,并部署在Azure上。因为业务逻辑不会太复杂,所以我想使用JHipster作为入门套件。该应用程序将有两种类型的用户:“办公室”用户,希望使用Office 365帐户登录应用程序,以及“普通”用户,希望使用他们的社交帐户,如谷歌或脸书,或者只是创建一个新的本地帐户。所有帐户应由Azure管理,没有密码应该存储在我们的数据库中。 第一个问题是关于Azure:我应该使用哪

  • JHipster 是一个 Yeoman 的生成器, 用来创建基于 Maven + Spring + AngularJS 的项目,提供完全热加载的 Java 和 JavaScript 代码。 我们的目的是生成一个完整和现代的 Web app。 服务器端技术堆栈: Spring Boot for easy application configuration Maven or Gradle config

  • Greetings, Java Hipster! Full documentation and information is available on our website at https://www.jhipster.tech/ Please read our guidelines before submitting an issue. If your issue is a bug, ple

  • Greetings, Java Hipster! This is the JHipster Angular 2+ utilities library This library is used by the code generated by the generator-jhipster: https://github.com/jhipster/generator-jhipster/. When r