【问题标题】:SpringBoot : getting error while uploading fileSpring Boot:上传文件时出错
【发布时间】:2019-01-25 09:50:51
【问题描述】:

使用 spring-boot 我试图发布图像,但我得到了

org.thymeleaf.exceptions.TemplateInputException

实体类

@Entity
@Table(name="image")
public class ImageEntity {

@Id
@Column(name="imageId")
private String imageId;

@Column(name="imageName")
private String imageName;

@Column(name="type")
private String type;

/*@Column(name="size")
private long size;*/

@Column(name="imagepath")
private String path;

public ImageEntity(String imageName, String type,  String path) {
    super();
    this.imageName = imageName;
    this.type = type;
    //this.size = size;
    this.path = path;
}

控制器类

   @Controller
   public class ImgContr {
    public static final Logger logger =LoggerFactory.getLogger(ImgContr.class);

@Autowired
public ImgService imgService;


@PostMapping("/addImage")
public ImageEntity saveImage(@RequestBody ImageEntity imgent, RedirectAttributes redirectAttributes) throws Exception 
{
return imgService.saveImage(imgent );
}

域服务

@Service
public class ImgService {

@Autowired
public ImageDao imageDao;


public ImageEntity saveImage(ImageEntity imgent) {
    ImageEntity imgEngDom=new ImageEntity();
    imgEngDom.setImageId(imgent.getImageId());
    imgEngDom.setImageName( imgent.getImageName());
    imgEngDom.setPath(imgent.getPath());
    //imgEngDom.setSize(imgent.getSize());
    imgEngDom.setType(imgent.getType());

    return imageDao.saveImage(imgEngDom);

}

ImageDAO.java

@Repository
public class ImageDao {

@PersistenceContext
private EntityManager entityManager;

@Autowired
SessionFactory sessionFactory;

public ImageEntity saveImage(ImageEntity imgEngDom) {

        Session session = null;

        try {
            session = sessionFactory.openSession();
            session.beginTransaction();
            session.save(imgEngDom);

            session.getTransaction().commit();
        } catch (Exception e) {
            session.getTransaction().rollback();


        } finally {
            session.close();
        }
        return imgEngDom;

}

负载请求。

  {
    "imageName": "Divya",
    "type" : "jpg",
    "path": " C:/Users/admin/Desktop"
  }

//如果我尝试在邮递员中发布这样的图像,我得到了错误

错误

{

  "timestamp": 1548408353973,
  "status": 500,
  "error": "Internal Server Error",
  "exception": "org.thymeleaf.exceptions.TemplateInputException",
  "message": "Error resolving template \"addImage\", template might not 
  exist or might not be accessible by any of the configured Template 
  Resolvers",
  "path": "/addImage"

}

我是springboot 的新手,我错了。帮帮我。

【问题讨论】:

  • 您的项目是支持 mvc 还是支持 REST-Api ?如果您希望您的控制器用作休息 API,请使用以下解决方案。它必须工作。 @divya你

标签: spring-boot


【解决方案1】:

我认为你的控制器配置有问题。

试试这个

@RestController
public class ImgContr {

而不是

@Controller
public class ImgContr {

更多详细内容请关注>>Controller vs RestController

注意:当您需要 json 响应而不是 Spring-mvc 项目的解决方案时,上述解决方案有效。

【讨论】:

  • 我已经尝试清除错误但 imgId 没有自动递增..如果我把@GeneratedValue(strategy=GenerationType.AUTO) 这个放在实体类中......并且所有记录都没有保存在数据库
  • @divyau 如果您仍然面临其他问题或错误或异常。继续使用适当的代码和错误堆栈发布新问题...
  • 现在我面临另一个错误,即....imageId 不是自动生成的,即使我将 @GeneratedValue(strategy=GenerationType.AUTO) 这个放在实体类中...并且所有数据都不是存储在数据库中
  • 没有用先生..我得到这个错误字段'image_id'没有默认值,如果我添加IDENTITY而不是AUTO..
  • 我需要 AUTO 或 IDENTITY..in 数据库我已经检查过 ImageId 不是 AutoIncremented 如果我尝试更改它不占​​用 AI 的表 ..
猜你喜欢
  • 1970-01-01
  • 2020-07-13
  • 2017-12-30
  • 2019-06-25
  • 1970-01-01
  • 2019-03-25
  • 2018-04-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多