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

soat7uwm  于 2023-03-07  发布在  Spring
关注(0)|答案(1)|浏览(139)

我正在学习spring boot,并且正在进行一个名为DocumentWeb的项目,它是一个简单的Web项目,包含一个控制器、一个jsp模式和一个扩展JpaRepository的文档存储库(接口
该项目应使用户能够上传图片,也下载任何图片已上传,并将其保存到数据库作为blob。但不运行在浏览器上。当我曾经coment了在控制器中的文档库的实现它运行finelly。但没有错误的代码。请帮助🙏🙏🙏
这是我的代码:

//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>
ggazkfy8

ggazkfy81#

在Repository java接口中使用@Repository注解。
和检查服务器日志。日志可能会提供有关错误的更详细信息,如导致问题的特定代码行。

相关问题