Question:
I’m currently working on a Spring Boot application where I have a News
entity with fields like title
, description
, date
, and image
. Initially, I was able to send data for title
, description
, and date
without any issues. Now, I’m trying to add an image upload feature using Axios, but I’m encountering a 500 error.
Below are my entity and controller classes. Could someone please help me understand what might be causing this issue with Axios? Has anyone encountered and fixed a similar problem before?
Entity Class:
package com.stephanodev.springsecurity.news;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Null;
import java.sql.Blob;
import java.util.List;
@Entity
public class News {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Null
private Long id;
@NotBlank
private String title;
private String description;
@Lob
private Blob image;
private String date;
private String category;
private String url;
@OneToMany(cascade = CascadeType.ALL)
@Column(columnDefinition = "TEXT")
private List<NewsDetail> details;
// Getters and setters
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
public Blob getImage() { return image; }
public void setImage(Blob image) { this.image = image; }
public String getDate() { return date; }
public void setDate(String date) { this.date = date; }
public String getCategory() { return category; }
public void setCategory(String category) { this.category = category; }
public String getUrl() { return url; }
public void setUrl(String url) { this.url = url; }
public List<NewsDetail> getDetails() { return details; }
public void setDetails(List<NewsDetail> details) { this.details = details; }
}
Controller Class:
package com.stephanodev.springsecurity.news;
@RequestMapping("/api/v1/news")
@RestController
public class NewsController {
@Autowired
private NewsRepository newsRepository;
@GetMapping
public ResponseEntity<Page<News>> getNews(@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int per_page) {
Pageable pageable = PageRequest.of(page, per_page, Sort.by("id").descending());
Page<News> news = newsRepository.findAll(pageable);
return ResponseEntity.ok(news);
}
@GetMapping("/all")
public ResponseEntity<List<News>> getAllNews() {
List<News> allNews = newsRepository.findAll(Sort.by(Sort.Direction.DESC, "id"));
return ResponseEntity.ok(allNews);
}
@PostMapping
public ResponseEntity<List<News>> createNews(@RequestBody @Valid List<News> newsList) {
List<News> savedNews = newsRepository.saveAll(newsList);
return ResponseEntity.status(HttpStatus.CREATED).body(savedNews);
}
}
I’m encountering a 500 error when attempting to upload an image using Axios. Any insights or suggestions would be greatly appreciated!
Hamza Oguz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.