Web 项目未在浏览器上运行(错误 500)

问题描述 投票:0回答:1

我正在学习 spring boot,我在一个名为 DocumentWeb 的项目上,它是一个简单的 Web 项目,带有控制器、jsp 模式和扩展 JpaRepository 的文档存储库(接口)

该项目应该允许用户上传图片并下载任何已上传的图片并将其作为 blob 保存到数据库中。但不能在浏览器上运行。 每当我注释掉控制器中文档存储库的实现时,它运行良好。但代码中没有错误。请帮助🙏🙏🙏

这是我的代码:

//Controller
package com.javaMadeEasy.document.controllers;

import com.javaMadeEasy.document.entities.Document;
import com.javaMadeEasy.document.repository.DocumentRepository;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

import java.io.IOException;
import java.util.List;

@Controller
public class DocumentController {
    @Autowired
    DocumentRepository repository;

    @RequestMapping("/displayUpload")
    public String displayUpload(ModelMap modelMap){
        extracted(modelMap);
        return "DocumentUpload";
    }



    @RequestMapping(value="/upload", method = RequestMethod.POST)
    public String uploadDocument(@RequestParam("document") MultipartFile multipartFile, @RequestParam("id") Long id, ModelMap modelMap){
//       multipartFile.getName();
        Document document = new Document();
        document.setId(id);
        document.setName(multipartFile.getOriginalFilename());
        try {
            document.setData(multipartFile.getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        repository.save(document);

        extracted(modelMap);
        return "DocumentUpload";
    }
    @RequestMapping("/download")
    public StreamingResponseBody download(@RequestParam("id") long id, HttpServletResponse response){
        Document document =repository.getOne(id);
        byte[] data  = document.getData();

        response.setHeader("Content-Disposition","attachment;filename=download.jpeg");

        return outputStream -> {
            outputStream.write(data);
        };
    }


    private void extracted(ModelMap modelMap) {
        List<Document> documents = repository.findAll();
        System.out.println(documents.size());
        modelMap.addAttribute("documents",documents);
    }
}

//Model
package com.javaMadeEasy.document.entities;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Lob;

@Entity
public class Document {
    @Id
    private Long id;
    private String name;
    @Lob
    private byte[] data;


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public byte[] getData() {
        return data;
    }

    public void setData(byte[] data) {
        this.data = data;
    }
}

//Repository
package com.javaMadeEasy.document.repository;

import com.javaMadeEasy.document.entities.Document;
import org.springframework.data.jpa.repository.JpaRepository;

public interface DocumentRepository extends JpaRepository<Document,Long> {
}

//application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/projectdb
spring.datasource.username=devuser
spring.datasource.password=Mamzy123

spring.jpa.show-sql=true
spring.mvc.view.prefix=/WEB-INF/jsps/
spring.mvc.view.suffix=.jsp

server.servlet.context-path=/documentWeb

logging.level.sql=debug
logging.level.web=debug
#logging.level.root=debug


//DocumentUpload.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
         pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Document Upload</title>
</head>
<h3>Upload A Photo</h3>
<body>

<form action="upload" method="post" enctype="multipart/form-data">
    <pre>
        Id:<input type="text" name="id"/>
        Document:<input type="file" name="id"/>
        <input type="submit" value="Upload"/>
        <input type="reset" value="refresh"/>
    </pre>
</form>
<table>
    <tr>
        <th>id</th>
        <th>name</th>
        <th>link</th>
    </tr>
    <c:forEach items="${documents}" var="document">
        <tr>
            <td>${document.id}</td>
            <td>${document.name}</td>
            <td><a href="download?id=${document.id}">download</a></td>
        </tr>
    </c:forEach>
</table>

</body>
</html>

//pom.xml



<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.javaMadeEasy.document</groupId>
    <artifactId>documentWeb</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>documentWeb</name>
    <description>Vendor Module</description>
    <properties>
        <java.version>19</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>6.0.4</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>


java spring spring-boot jstl jparepository
1个回答
0
投票

在 Repository java 接口中使用 @Repository 注释。

并检查服务器日志。日志可能会提供有关错误的更多详细信息,例如导致问题的特定代码行。

© www.soinside.com 2019 - 2024. All rights reserved.