赞
踩
使用springboot在接口内直接返回图片,生成url地址,浏览器直接访问
需要数据库与实体类 ,下面详细介绍使用方法
获取后端接口返回的数据
复制返回的链接在浏览器打开
接口
- @Controller
- @RequestMapping("/app")
- public class FirmController {
- @Autowired
- private FirmMapper firmMapper;
- //获取全部的厂商
- @GetMapping("/list")
- @ResponseBody
- public Object ListFirm() {
- //查询数据
- List<Firm> firms = firmMapper.selectList(null);
-
- /** 测试实例
- for (Firm firm : firms) {
- System.out.println(firm);
- }*/
- //判断接口内容是否为空
- if (firms.size() != 0) {
- //遍历图片
- for (Firm firm : firms) {
- //获取图片的名称, 再用ServletUtils生成url地址
- firm.setImage(ServletUtils.getImageUrl(firm.getImage()));
- }
- return JSONObject.toJSON(new ReturnObject(200, "查询成功", firms.size(), firms));
- } else {
- return JSONObject.toJSON(new ReturnObject(500, "查询失败"));
- }
- }
SerfletUtils工具类: 主要功能是生成图片的url地址信息 (自动拼接url)
- import org.springframework.web.context.request.RequestContextHolder;
- import org.springframework.web.context.request.ServletRequestAttributes;
-
- import javax.servlet.http.HttpServletRequest;
-
- public class ServletUtils {
- //生成当前请求的url 最后在接口中拼接url 生成一个新的访问地址
- public static String getImageUrl(String imageName){
- ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
- HttpServletRequest request = servletRequestAttributes.getRequest();
- StringBuffer requestURL = request.getRequestURL();
- String servletPath = request.getServletPath();
- int index = requestURL.indexOf(servletPath);
- return requestURL.delete(index+1 , requestURL.length())+"images/"+imageName;
- }
-
- }
实体类
- package com.gong.app_school.model.dao;
-
- import com.baomidou.mybatisplus.annotation.IdType;
- import com.baomidou.mybatisplus.annotation.TableId;
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- public class Firm {
- @TableId(value = "id", type = IdType.AUTO)
- private int id;
- private String name;
-
- private String news;
-
- private String classify;
-
- private String event;
- //图片
- private String image;
-
- /**
- * 1推荐活动 0 不推荐 int类型
- */
- private Integer recommand;
-
-
- }
数据库
数据库内直接存放图片名称即可
存放图片的路径一定要在 resources 下 static 下 images中
resources/static/images
springboot使用接口返回图片: 图片的名称存在数据库中(image在数据库的类型为varchar,数据库只起到存放图片名称的作用 ),图片存放在resources/staic/images内,编写实体类,然后在接口内主要遍历数组内数据,遍历完后通过servletUitls获取生成图片的url地址,主义返回类型为json格式。
pom.xml
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <scope>runtime</scope>
- </dependency>
- <!--mybatis-plus -->
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-boot-starter</artifactId>
- <version>3.0.5</version>
- </dependency>
-
- <!--lombok-->
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <version>1.18.10</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>fastjson</artifactId>
- <version>1.2.9</version>
- </dependency>
-
- </dependencies>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。