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

Ashen Jayawardena:

我正在构建我的第一个springboot微服务项目,并尝试将请求发布到订阅者服务,该请求需要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);
 }

};

我在主类中按如下方式调用了该方法,因为我希望在启动服务器时执行该方法,

@SpringBootApplication
public class OrderGeneratorServiceApplication {

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

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

Madhu Sharma:

如果您使用的是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", "[email protected]");

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

        System.out.println(result);
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TOP 榜单

热门标签

归档