0

I am trying to upload json data and image to a database of a form using spring rest and hibernate. I tried to test it using POSTMAN by setting form-data in body and content-type as application/json in header, but i am getting http error 400. I also tried using @RequestPart but didnt not work. I searched but could not find an example using ResponseEnity<>. I think i am doing something wrong in controller class. Please someone help me.

Without the file part i am able to add json data to db using this.

@RequestMapping(value = "/users", method = RequestMethod.POST, produces ="application/json")
     public   ResponseEntity<User> createAparts(  @RequestBody User user) {
      if (user == null) {
       return new ResponseEntity<User>(HttpStatus.BAD_REQUEST);
      }
      userService.addAparts(user);
      return new ResponseEntity<User>(user, HttpStatus.CREATED);
     }

Below are the related code to issue.

model

@Entity
@Table(name = "User")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler", "ignoreUnknown = true"})
public class User{

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    @Column(name = "Name")
    private String Name;

    @Column(name = "file_data")
    private byte[] file_data;

    @Column(name = "filename")
    private String filename;

    @JsonCreator
    public ApartsData(@JsonProperty("id") int id, 
                  @JsonProperty("Name") String Name,
                  @JsonProperty("filename") String filename,
                  @JsonProperty("file_data") byte[] file_data){
        this.ad_id = ad_id;
        this.Name = Name;
        this.filename= filename;
        this.file_data = file_data;
    }
    public User(){
        }

DAO

@Repository
public class UserDaoImpl implements UserDao{
    private SessionFactory sessionFactory;
    public void setSessionFactory(SessionFactory sessionFactory){
    this.sessionFactory = sessionFactory;
    }
    @Override
    public void addUser(User user) {
        Session session = this.sessionFactory.getCurrentSession();
        session.persist(user);
    }
}

Service

@Service
public class UserServiceImpl implements UserService {
    private UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    @Override
    @Transactional
    public void addUser(User user) {
        this.userDao.addUser(user);
    }
}

controller

    @RestController
    public class UserController {
    private UserService userService; 

        @Autowired(required=true)
        @Qualifier(value="userService")
        public void setUserService(UserService userService){
            this.userService = userService;
        }

        @RequestMapping(value = "/users", method = RequestMethod.POST, 
                                           produces ="application/json")
         public ResponseEntity<User> createApartsData(@RequestBody User user, 
                  @RequestParam("file") MultipartFile file) {

             HttpHeaders headers = new HttpHeaders();
              if (user == null) {
               return new ResponseEntity<User>(HttpStatus.BAD_REQUEST);
              }
              if (!file.isEmpty()) {
                    try {
                        user.setFilename(file.getOriginalFilename());
                        user.setFile_data(file.getBytes());
                        } catch (Exception e){
                        e.printStackTrace();
                    }
                }
              userService.addUser(user);
              headers.add("User Created  - ", String.valueOf(user.getid()));

              return new ResponseEntity<User>(user, headers, HttpStatus.CREATED);
        }
    }

UPDATE: I am able to make it work with @RequestParam. Please some help me to make it work with @RequestBody

user7620991
  • 97
  • 1
  • 11

0 Answers0