Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在springboot microservice体系结构中,带有JSON主体的POST请求不会传递到端点_Java_Json_Spring Boot_Rest - Fatal编程技术网

Java 在springboot microservice体系结构中,带有JSON主体的POST请求不会传递到端点

Java 在springboot microservice体系结构中,带有JSON主体的POST请求不会传递到端点,java,json,spring-boot,rest,Java,Json,Spring Boot,Rest,我正在构建我的第一个springboot microservice项目,我正在尝试向订阅者服务发布请求,这需要一个json格式的主体,并且必须包含订阅服务的名称和uri才能到达订阅者,这是实现POST请求的类 public class Registration{ public static void postRegistration(){ final String registrationUrl = "localhost:9000/registry"; RestTempl

我正在构建我的第一个springboot microservice项目,我正在尝试向订阅者服务发布请求,这需要一个json格式的主体,并且必须包含订阅服务的名称和uri才能到达订阅者,这是实现POST请求的类

public class Registration{

public static void postRegistration(){

final String registrationUrl = "localhost:9000/registry";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
JSONObject registrationDetails = new JSONObject();

headers.setContentType(MediaType.APPLICATION_JSON);
registrationDetails.put("name", "OrderGenerator");
registrationDetails.put("uri", "http://localhost:8081/generate");
HttpEntity<String> request = 
new HttpEntity<String>(registrationDetails.toString(), headers);

String response = restTemplate.postForObject(registrationUrl,request,String.class);
System.out.println(response);
 }

};

我的编译器没有显示任何错误,但是我没有收到我提供的url的通知,但是当我通过邮递员发送POST请求时,它开始工作。我做错了什么?

如果您使用的是Postman,它会为您的请求创建自动生成的代码。它位于右侧“保存”按钮的正下方。您需要创建自己的自动格式化方法,以便提供正确的输入。下面是我目前使用的一些API的示例

你知道我是如何为第三行输入超长信息的吗?您应该创建一个方法,该方法将接受您的输入并自动格式化它

                OkHttpClient client = new OkHttpClient().newBuilder()
                        .build();
                MediaType mediaType = MediaType.parse("application/json");
                RequestBody body = RequestBody.create(mediaType, "{\r\n  \"categoryName\": \"PostMan\",\r\n  \"categoryQuestions\": [\r\n    \"When is the last time you experienced nostalgia?\",\r\n    \"What's the scariest dream you've ever had?\",\r\n    \"What's the weirdest thought you've ever had?\",\r\n    \"What's the first thing that comes to mind when you hear the word fidget?\",\r\n    \"What made-up word would you incorporate into the English language if you could?\"\r\n  ]\r\n}");
                Request request = new Request.Builder()
                        .url("http://localhost:8080/api/v1/categories")
                        .method("POST", body)
                        .addHeader("Content-Type", "application/json")
                        .build();
                Response response = client.newCall(request).execute();
                System.out.println(response.body().string());
下面是我的示例(我正在使用JSON数据,格式如下

{
  "categoryName": "PostMan",
  "categoryQuestions": [
    "When is the last time you experienced nostalgia?",
    "What's the scariest dream you've ever had?",
    "What's the weirdest thought you've ever had?",
    "What's the first thing that comes to mind when you hear the word fidget?",
    "What made-up word would you incorporate into the English language if you could?"
  ]
}
这就是我格式化数据的方式

public static String inputParse(String nameInput, ArrayList<String> questionInput) {
        String inputBuilder = "{\r\n  \"categoryName\": \"" + nameInput + "\",\r\n  \"categoryQuestions\": [\r\n    ";
        for (int i = 0; i < questionInput.size(); i++)
        {
            inputBuilder += "\"" + questionInput.get(i) + "\"";
            if (i != questionInput.size()-1)
            {
                inputBuilder += ",";
            }
        }
        inputBuilder += "\r\n  ]\r\n}";
        return inputBuilder;
    }

如果您使用的是Postman,它会为您发出的请求创建自动生成的代码。它位于右侧“保存”按钮的正下方。您需要创建自己的自动格式化方法,以便提供正确的输入。下面是我当前使用的某个API的示例

你看到我如何为第三行输入超长的内容了吗?你应该创建一个方法来接受你的输入并自动格式化它

                OkHttpClient client = new OkHttpClient().newBuilder()
                        .build();
                MediaType mediaType = MediaType.parse("application/json");
                RequestBody body = RequestBody.create(mediaType, "{\r\n  \"categoryName\": \"PostMan\",\r\n  \"categoryQuestions\": [\r\n    \"When is the last time you experienced nostalgia?\",\r\n    \"What's the scariest dream you've ever had?\",\r\n    \"What's the weirdest thought you've ever had?\",\r\n    \"What's the first thing that comes to mind when you hear the word fidget?\",\r\n    \"What made-up word would you incorporate into the English language if you could?\"\r\n  ]\r\n}");
                Request request = new Request.Builder()
                        .url("http://localhost:8080/api/v1/categories")
                        .method("POST", body)
                        .addHeader("Content-Type", "application/json")
                        .build();
                Response response = client.newCall(request).execute();
                System.out.println(response.body().string());
下面是我的示例(我正在使用JSON数据,格式如下

{
  "categoryName": "PostMan",
  "categoryQuestions": [
    "When is the last time you experienced nostalgia?",
    "What's the scariest dream you've ever had?",
    "What's the weirdest thought you've ever had?",
    "What's the first thing that comes to mind when you hear the word fidget?",
    "What made-up word would you incorporate into the English language if you could?"
  ]
}
这就是我格式化数据的方式

public static String inputParse(String nameInput, ArrayList<String> questionInput) {
        String inputBuilder = "{\r\n  \"categoryName\": \"" + nameInput + "\",\r\n  \"categoryQuestions\": [\r\n    ";
        for (int i = 0; i < questionInput.size(); i++)
        {
            inputBuilder += "\"" + questionInput.get(i) + "\"";
            if (i != questionInput.size()-1)
            {
                inputBuilder += ",";
            }
        }
        inputBuilder += "\r\n  ]\r\n}";
        return inputBuilder;
    }

我已经在使用RestTemplate了,在我的实现中传递JSON字符串的方式是否有错误?因为我所做的几乎是一样的,只是我完全错过了postman代码部分,我添加了okhttp依赖项并复制粘贴了代码,瞧,成功了!谢谢你的帮助!我已经在使用RestTemplate了,有问题吗我在我的实现中传递JSON字符串的方式出错?因为我所做的几乎是一样的,只是我完全错过了postman代码部分,我添加了okhttp依赖项并复制粘贴了代码,瞧,成功了!感谢您的帮助!