繁体   English   中英

带有 JSON 主体的 POST 请求不会传递到 springboot 微服务架构中的端点

[英]POST request with JSON body do not deliver to the endpoint in springboot microservice architecture

我正在构建我的第一个 springboot 微服务项目,我正在尝试向订阅者服务发布请求,该请求需要 json 格式的主体,并且必须包含订阅服务的名称和 uri 才能到达订阅者,这是class 实现 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);
 }

};

我在主 class 中调用了该方法,如下所示,因为我希望它在我启动服务器时执行,

@SpringBootApplication
public class OrderGeneratorServiceApplication {

public static void main(String[] args) {
    
    SpringApplication.run(OrderGeneratorServiceApplication.class, args);
    Registration.postRegistration();
}

我的编译器没有,没有显示任何错误,但我没有收到我提供的 url 的通知,但是当我通过 postman 发送 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;
    }

另外,我看到您正在使用 Springboot。 您可以使用 org.springframework.web.client.RestTemplate (rest-template) 来消费 rest api。 使用我从网站获得的 REST 模板的示例。

private void createEmployee() {

        Employee newEmployee = new Employee("admin", "admin", "admin@gmail.com");

        RestTemplate restTemplate = new RestTemplate();
        Employee result = restTemplate.postForObject(CREATE_EMPLOYEE_ENDPOINT_URL, newEmployee, Employee.class);

        System.out.println(result);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM