1

I'm saving images to the database using spring boot, the image url is also displayed on postman when I send it but the issue is that when I copy the image url link to the browser, the image is not showing rather it's showing This application has no explicit mapping for /error, so you are seeing this as a fallback.What is wrong with it.

This is my Entity class

@Data
@Entity
@NoArgsConstructor
public class Attachment {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String fileName;
    private String fileType;
    @Lob
    private byte[] data;

    public Attachment(String fileName, String fileType, byte[] data) {
        this.fileName = fileName;
        this.fileType = fileType;
        this.data = data;
    }
}

This is the dto class

@Data
@NoArgsConstructor
@AllArgsConstructor
public class AttachmentDto {

    private String fileName;
    private String downloadUrl;
    private String fileType;
    private long fileSize;
}
        

This is my controller class

@RestController
public class AttachmentController {


    private AttachmentService attachmentService;

    public AttachmentController(AttachmentService attachmentService) {
        this.attachmentService = attachmentService;
    }


@PostMapping("/upload")
    public ResponseEntity<AttachmentDto> uploadFile(@RequestParam("file")MultipartFile file) throws Exception {
        Attachment attachment = attachmentService.saveFile(file);
        String downloadUrl = ServletUriComponentsBuilder.fromCurrentContextPath()
                .path("/profileImage")
                .path(attachment.getFileName())
                .toUriString();

        return new ResponseEntity<>(new AttachmentDto(attachment.getFileName(),downloadUrl,file.getContentType(),file.getSize()), HttpStatus.CREATED);
    }
}
        

Here is my service class

@Service
public class AttachmentService {


    private AttachmentRepository attachmentRepository;

    public AttachmentService(AttachmentRepository attachmentRepository) {
        this.attachmentRepository = attachmentRepository;
    }

    public Attachment saveFile(MultipartFile file) throws Exception {
        String fileName = StringUtils.cleanPath(file.getOriginalFilename());
        try {
            if (fileName.contains("..")){
                throw new Exception("File Name contains invalid sequence");
            }
            Attachment attachment = new Attachment(fileName,file.getContentType(),file.getBytes());
            return attachmentRepository.save(attachment);
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception("Could not save file "+fileName);

        }
    }
}

The image was saved successfully in postman and this was returned

"fileName": "IMG_20180312_131559.jpg",
    "downloadUrl": "http://localhost:8080/profileImageIMG_20180312_131559.jpg",
    "fileType": "image/jpeg",
    "fileSize": 1061724

I copied the downloadUrl into the browser but the image is not showing nor downloading but rather showing This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Sep 26 11:11:52 PDT 2022
There was an unexpected error (type=Not Found, status=404).
M. Deinum
  • 115,695
  • 22
  • 220
  • 224
Ukeme Elijah
  • 157
  • 3
  • 13
  • what is the link ??;) – Antoniossss Sep 26 '22 at 09:49
  • here is the link "downloadUrl": "http://localhost:8080/profileImageIMG_20180312_131559.jpg". I thought that the image supposed to be displayed when I paste the link in the browser? – Ukeme Elijah Sep 26 '22 at 10:31
  • you would have to configure static resource resolver to do that as for now, there is no mapping for that url - which is what you can clrearly see. Alternatively create @GetMapping(*) and resolve images by hand – Antoniossss Sep 26 '22 at 10:33
  • The URL doesn't have proper path segments and there is no `@GetMapping` so nothing will listen to it. – M. Deinum Sep 26 '22 at 12:00

0 Answers0