簡體   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