SpringBoot:上传文件时出错

时间:2019-01-25 09:50:51

标签: spring-boot

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

cost myOtherHandler = () => { myHandler(); // ...other stuff }

  

实体类

inputStreamCsv
  

控制器类

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;
}
  

ImageDAO.java

   @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);

}

///如果我尝试在邮递员中发布以下图像,我会报错

  

错误

{

@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" } 的新手,我错了。救救我。

1 个答案:

答案 0 :(得分:0)

我认为您的Controller配置有问题。

尝试

@RestController
public class ImgContr {

代替

@Controller
public class ImgContr {

有关更多详细信息,请关注>> Controller vs RestController

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