gpt4 book ai didi

java - springboot 返回 responseentity 返回 JSON

转载 作者:行者123 更新时间:2023-11-29 08:28:11 26 4
gpt4 key购买 nike

我想在下面返回 JSON。

{ "name": "jackie" }

postman 给我错误。说明

Unexpected 'n'

这里是 Spring Boot 的新手。 1日龄。有没有正确的方法来做到这一点?

   // POST method here
@RequestMapping(method = RequestMethod.POST , produces = "application/json")
ResponseEntity<?> addTopic(@RequestBody Topic topic) {

if (Util.save(topicRepository, new Topic(topic.getTopicName(), topic.getQuestionCount())) != null) {
return Util.createResponseEntity("Name : jackie", HttpStatus.CREATED);
}
return Util.createResponseEntity("Error creating resource", HttpStatus.BAD_REQUEST);
}

最佳答案

这是我用的:

@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Map<String, String>> hello() {
try {
Map<String, String> body = new HashMap<>();
body.put("message", "Hello world");
return new ResponseEntity<>(body, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}

关于java - springboot 返回 responseentity 返回 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50654291/

26 4 0