1

For the below code when I am trying to hit through through postman it is working fine when the file is attached only. But when a file is not attached it is throwing an internal server error it was there below. but in my case, if the file is not attached also it needs to work. Thanks in advance

@PostMapping("add")
public ResponseEntity<JSONObject> addProject(
    @RequestPart(required = false, value = "file")  MultipartFile file,
    @RequestPart(value = "jsonObject", required = false) JSONObject jsonObject) {
        JSONObject responseJsonObject = null;
        try {
            responseJsonObject = service.addProject(request, response, file, jsonObject);
      } catch (Exception e) {
        LOGGER.error("Error in addProject() method !!", e);
      }
      return new ResponseEntity<JSONObject>(responseJsonObject, HttpStatus.CREATED);
}

The response in postman when the file is not attached:

<!doctype html>
<html lang="en">

<head>
    <title>HTTP Status 500 – Internal Server Error</title>
    <style type="text/css">
        body {
            font-family: Tahoma, Arial, sans-serif;
        }

        h1,
        h2,
        h3,
        b {
            color: white;
            background-color: #525D76;
        }

        h1 {
            font-size: 22px;
        }

        h2 {
            font-size: 16px;
        }

        h3 {
            font-size: 14px;
        }

        p {
            font-size: 12px;
        }

        a {
            color: black;
        }

        .line {
            height: 1px;
            background-color: #525D76;
            border: none;
        }
    </style>
</head>

<body>
    <h1>HTTP Status 500 – Internal Server Error</h1>
</body>

</html>
João Dias
  • 16,277
  • 6
  • 33
  • 45
  • 1
    try `@RequestParam(value ="file", required=false) MultipartFile file, @Valid @ModelAttribute JSONObject jsonObject` or `@RequestParam(value ="file", required=false) MultipartFile file, @RequestParam(value ="jsonObject", required=false) JSONObject jsonObject` – navnath Sep 05 '21 at 06:38
  • when I am trying in the first way JSONObject is coming null.@Navnath Jadhaav can you explain how will we send requests through PostMan for this controller – Akhil Nallapaneni Sep 05 '21 at 07:13
  • @RequestPart( value="file",required = false) MultipartFile file,@RequestPart(value="jsonObject")JSONObject jsonObject) this form is working – Akhil Nallapaneni Sep 06 '21 at 03:03

0 Answers0