gpt4 book ai didi

java - 我的 postman 发帖请求应该是什么样子? (帖子请求的正文)

转载 作者:行者123 更新时间:2023-12-02 09:28:07 24 4
gpt4 key购买 nike

@PostMapping(path="/check/keywords")
@RequestMapping(value = "/check/keywords", method = RequestMethod.POST)
public int totalKeywords(@RequestBody String text,@RequestBody String[] keywords) throws Exception{
System.out.println(text);
System.out.println(keywords.length);
return EssayGrader.totalKeywods(text);
}

I have tried various different body for this request but nothing seems to work. Either it gives error 400 or 500 internal server error. I want to pass a text of type string and some keywords in the form of list or array from the Html page to my java code to see how many keywords are there in that text string. Can you please help me.

最佳答案

我相信在方法级别只能有一个 @RequestBody 带注释的参数。我建议将文本和关键字作为 JSON 文档发送到 HTTP 请求的请求正文中。例如。可能看起来像这样:

{
"text": "一个简单的文本",
“关键字”:[“简单”,“文本”]
}

编写一个简单的 Java 类来保存这两个值,例如

class Data {
String text;
String[] keywords;

//don't forget getter + setter + noargs constructor
}

将方法更改为:

@PostMapping(path="/check/keywords", consumes="application/json", produces="application/json")
public int totalKeywords(@RequestBody Data data) {
String text = data.getText();
String[] keywords = data.getKeywords();
// do whatever...
return ...
}

当您发送 Postman POST 请求时,将 Content-Type header 设置为 application/json!并确保您的类路径上有 Jackson 库,用于“自动”将 JSON 结构映射到 Java 类/对象(如果您使用 Spring Boot,则在导入此依赖项时应该已经是这种情况:

    <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

希望这有帮助。祝你好运!

关于java - 我的 postman 发帖请求应该是什么样子? (帖子请求的正文),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58188310/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com