I want to create some set of questions and answers im able pass the code as json structure but it is not working as form data in postman so giving this following error or i don't know how to send the below code in the form-data anyone help me
{
"id": 1,
"title": "Html",
"description": "Hyper text markup language",
"category": "frontEnd",
"tag": "html",
"image": "html.jpg",
"questions": [
{
"question": "Where html belong to",
"optionA": "frontEnd",
"optionB": "backEnd",
"optionC": "database",
"optionD": "All the Above",
"answer": "frontEnd"
},
{
"question": "Where Java belong to",
"optionA": "frontEnd",
"optionB": "backEnd",
"optionC": "database",
"optionD": "All the Above",
"answer": "backEnd"
}
]
}
Im trying to pass like this ,click on this image to see postman
@RestController
@RequestMapping("student/blog")
public class BlogRestController {
@PostMapping(value = "/upload-files",consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
@ResponseBody
public ResponseEntity<Object> saveBlog(
@RequestPart("image") MultipartFile multipartFile,
@RequestParam("title") String title,
@RequestParam("description") String description,
@RequestParam("category") String category,
@RequestParam("tag") String tag,
@RequestParam("questions") List<Question> questions
)
throws IOException {
Blog blog = new Blog();
String fileName = StringUtils.cleanPath(multipartFile.getOriginalFilename());
blog.setImage(fileName);
blog.setTitle(title);
blog.setDescription(description);
blog.setCategory(category);
blog.setTag(tag);
blog.setQuestions(questions);
blogRepo.save(blog);
"Required request parameter 'questions' for method parameter type List is not present" error code but if I try to send it in this way
"message": "Failed to convert value of type 'java.lang.String' to required type 'java.util.List'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.dfsfe.entities.Question': no matching editors or conversion strategy found", Here is My entity class
@Entity
@Table(name = "blog")
public class Blog {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "title",nullable = false)
private String title;
@Column(name = "description",nullable = false,columnDefinition = "TEXT")
private String description;
@Column(name = "category",nullable = false)
private String category;
@Column(name = "tag",nullable = false)
private String tag;
@Column(name = "image",nullable = false)
private String image;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "questions",referencedColumnName = "id")
private List<Question> questions;
// with getters and setters
if anyone know please answer this question