【问题标题】:How to return message in Resposne Body?如何在响应正文中返回消息?
【发布时间】:2022-11-11 22:19:44
【问题描述】:

我必须在 ResponseBody 的 api 中返回消息“已添加数据” 在输入学生数据的同时创建一个 api /新生

请求正文:

{
"name":"Shubham",
"rollno":22,
"studentid":1
}

回复:

{
"status":"OK",
"message":"Data Added"
}

@RequestMapping("/studentdata")
@ResponseBody
@ResponseStatus(HttpStatus.OK )

【问题讨论】:

    标签: java spring-boot


    【解决方案1】:

    您可以创建一个如下所示的自定义响应类:

    class CustomResponse {
       private String status;
       private String message;
       // Constructor/Getters/Setters
    }
    

    然后在您的控制器中返回 ResponseEntity 例如:

    CustomResponse response = new CustomResponse("OK", "Data Added");
    return ResponseEntity.ok(response); // the ok will return HTTP Status 200
    

    或者,如果您想要另一个 HttpStatus,那么您可以使用例如:

    return new ResponseEntity<>(response, HttpStatus.CREATED);
                                          ^^^^^^^^^^^^^^^^^^
    

    【讨论】:

    • 我必须在控制器包中创建类还是创建另一个类。
    • 这取决于您的代码架构,如果您打算在其他控制器中使用此自定义类,那么最好全局创建它,例如在包域中!
    【解决方案2】:

    这是如何返回自定义对象作为响应。

    router.post("/newStudent", async (req, res) => {
      const { name, rollNo, studentId } = req.data;
    
      // POST data to DB
      const result = await AddStudentDataToDB({ name, rollNo, studentId });
    
      res.status(200).json({
        status: 'ok',
        message: 'Data Added'
      });
    });
    

    【讨论】:

      【解决方案3】:

      首先,您应该创建一个 Response 类,它将包含状态代码和您的自定义消息,如下面的类:

      @Data
      @AllArgsConstructor
      @NoArgsConstructor
      public class Response {
        private String statusCode;
        private String statusMsg;
      }
      

      因此,在您发布对象的控制器中,使用 ResponseEntity 可以让您自定义 HTTP 响应方法。例如:

       @Autowired
       private StudentRepository studentRepository;
      
       @PostMapping("/newStudent")
       public ResponseEntity<Response> saveEmployee(@RequestBody Student 
        student){
          studentRepository.save(student);
          Response response = new Response();
          response.setStatusCode("200");
          response.setStatusMsg("Your message");
          return ResponseEntity.status(HttpStatus.CREATED).body(response);
      }
      

      【讨论】:

        【解决方案4】:
        import org.json.simple.JSONObject;
        
        @ResponseBody
        @RequestMapping(value = "/studentdata", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
        public String message(@RequestBody String transaction) {
          String response = "";
        
          JSONObject obj = new JSONObject();
          obj.put("status", "OK");
          obj.put("message", "Data Added");
          response = obj.toJSONString();
          
          return response;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-12-09
          • 2018-02-22
          • 2019-12-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多