I am trying to convert the uploaded file path in angular to a byte[] array after receiving it as a string in spring boot controller using @RequestParam. I am adding the byte array to object using simple setter in model class. while registering I am getting 400 HttpError Response
Model Class
@Entity
@Table(name="registration")
public class Registration {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private int id;
private String name;
private String username;
private String password;
@Lob
private byte[] image;
// respective getters,setters and constructor
My Registration Repository
@Repository
public interface RegistrationRepository extends JpaRepository<Registration, Integer> {
public List<Registration> findByUsernameAndPassword(String username,String password);
}
Registration Service is present
Registration Controller:-
@RestController
@RequestMapping("/register")
@ComponentScan
public class RegistrationController {
@Autowired
private RegistrationService res;
@PostMapping("/registeruser")
public ResponseEntity<Registration> registeruser(@RequestBody Registration reg, @RequestParam String filename) throws IOException
{
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
String userHashedPassword = bCryptPasswordEncoder.encode(reg.getPassword());
File file = new File(filename);
byte[] picInBytes = new byte[(int) file.length()];
FileInputStream fileInputStream = new FileInputStream(file);
fileInputStream.read(picInBytes);
fileInputStream.close();
reg.setImage(picInBytes);
Registration resm= new Registration(reg.getName(),reg.getUsername(),userHashedPassword, reg.getImage());
Registration userResp = res.registeruser(resm);
return new ResponseEntity<Registration>(userResp,HttpStatus.OK);
}