I'm working on a springboot project and I have entity called Event
, where I need to create a event with POST
request. When creating the event I need to upload company_logo
along with it. I wrote the necessary codes and tried the API with postman. But Unfortunately, I'm facing difficulty when trying to upload image with the json body.
Here is my Event Entity
package com.example.attendingsystem.entity;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import org.jetbrains.annotations.NotNull;
import java.time.LocalDateTime;
@Entity
public class Event {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotBlank(message = "Event Name cannot be blank")
private String eventName;
private String eventType;
@NotBlank(message = "Event location cannot be blank")
private String location;
@NotNull
private LocalDateTime startTime;
@NotNull
private LocalDateTime endTime;
@Lob
private byte[] company_logo;
@Lob
private byte[] event_flyer;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getEventName() {
return eventName;
}
public void setEventName(String eventName) {
this.eventName = eventName;
}
public String getEventType() {
return eventType;
}
public void setEventType(String eventType) {
this.eventType = eventType;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public LocalDateTime getStartTime() {
return startTime;
}
public void setStartTime(LocalDateTime startTime) {
this.startTime = startTime;
}
public LocalDateTime getEndTime() {
return endTime;
}
public void setEndTime(LocalDateTime endTime) {
this.endTime = endTime;
}
public byte[] getCompany_logo() {
return company_logo;
}
public void setCompany_logo(byte[] company_logo) {
this.company_logo = company_logo;
}
public byte[] getEvent_flyer() {
return event_flyer;
}
public void setEvent_flyer(byte[] event_flyer) {
this.event_flyer = event_flyer;
}
}
EventService class package com.example.azendattendingsystem.service;
import com.example.attendingsystem.dto.EventDTO;
import com.example.attendingsystem.entity.Event;
import com.example.attendingsystem.repository.EventRepo;
import jakarta.transaction.Transactional;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
@Transactional
public class EventService {
@Autowired
private EventRepo eventRepo;
@Autowired
private ModelMapper modelMapper;
public EventDTO saveEvent(EventDTO eventDTO){
eventRepo.save(modelMapper.map(eventDTO, Event.class));
return eventDTO;
}
}
And the EventController class package com.example.azendattendingsystem.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping(value="/api/event")
@CrossOrigin
public class EventController {
@Autowired
private EventService eventService;
@PostMapping("/saveEvent")
public EventDTO saveEvent(@RequestBody EventDTO eventDTO){
return eventService.saveEvent(eventDTO);
}
}
I'm beginner to springboot and I just need to know how to send image along with the json object in postman.
Here is the sample json object that I have tried:
{
"eventName":"Test Event -3",
"eventType":"Test Event Type -3",
"location":"Jaffna",
"startTime":"2019-01-13T17:08:00.411",
"endTime":"2019-01-13T17:10:00.411",
"company_logo":null,
"event_flyer": null
}
Can someone help me in this issue?
UPDATE
I have changed my EventService class as follows:
@Service
@Transactional
public class EventService {
@Autowired
private EventRepo eventRepo;
@Autowired
private ModelMapper modelMapper;
public Event saveEvent(byte[] company_logo, EventDTO eventDTO){
Event event=new Event();
event.setEventName(eventDTO.getEventName());
event.setEventType(eventDTO.getEventType());
event.setLocation(eventDTO.getLocation());
event.setStartTime(eventDTO.getStartTime());
event.setEndTime(eventDTO.getEndTime());
event.setCompany_logo(company_logo);
return eventRepo.save(event);
}
}
And EventController class as follows:
@RestController
@RequestMapping(value="/api/event")
@CrossOrigin
public class EventController {
@Autowired
private EventService eventService;
@PostMapping(value = "/saveEvent", consumes = {"multipart/form-data"})
public EventDTO saveEvent(@RequestParam(value = "company_logo", required = false) MultipartFile company_logo, @RequestBody EventDTO eventDTO) throws IOException {
byte[] logo = null; // or set to an empty byte array if preferred
if (company_logo != null && !company_logo.isEmpty()) {
logo = company_logo.getBytes();
}
// call the saveEvent method in your EventService class and pass the eventDTO and logo parameters
Event savedEvent = eventService.saveEvent( logo, eventDTO);
// create a new EventDTO object and populate it with the savedEvent data
EventDTO savedEventDTO = new EventDTO();
savedEventDTO.setEventName(savedEvent.getEventName());
savedEventDTO.setEventType(savedEvent.getEventType());
savedEventDTO.setLocation(savedEvent.getLocation());
savedEventDTO.setStartTime(savedEvent.getStartTime());
savedEventDTO.setEndTime(savedEvent.getEndTime());
// return the savedEventDTO object
return savedEventDTO;
}
}
And tried the postman for POST request. But I'm getting warning ,
WARN 21308 --- [nio-8080-exec-8] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content-Type 'multipart/form-data;boundary=--------------------------666943417236077820793463;charset=UTF-8' is not supported]