创建商品返回的实体类

  • id
  • 名称
  • 图片地址
  • 商品价格
1
2
3
4
5
6
7
public class Res_Product {

private int id;
private String name;
private String imgUrl;
private double price;
}

提供rest服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@Service
public class ProductServiceImpl implements ProductService{

@Autowired
ProductMapper productMapper;

@Override
public ResponseJsonResult getProduct() {

PageHelper.startPage(1,20);
ProductExample productExample = new ProductExample();
List<Product> products = productMapper.selectByExample(productExample);

List<Res_Product> res_products = new ArrayList<>();

for (int i = 0; i < products.size() ; i++) {
Res_Product res_product = new Res_Product();
Product product = products.get(i);
res_product.setPrice(product.getPrice());
res_product.setId(product.getId());
res_product.setName(product.getName());
res_product.setImgUrl(product.getImage());
res_products.add(res_product);
}

ResponseJsonResult responseJsonResult = new ResponseJsonResult();
responseJsonResult.setList(res_products);

return responseJsonResult;
}
}

portal 系统获取数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Service
public class ProductServiceImpl implements ProductService {

@Value("${REST_URL}")
private String REST_URL;
@Value("${REST_PRODUCT_LIST}")
private String REST_PRODUCT_LIST;

@Override
public List<Res_Product> getProducts() {

try {
String jsonResult = HttpClientUtil.httpGet(REST_URL + REST_PRODUCT_LIST);
ResponseJsonResult responseJsonResult = GsonUtils.fromJson(ResponseJsonResult.class, jsonResult);
List<Res_Product> list = (List<Res_Product>) responseJsonResult.getList();
return list;
} catch (IOException e) {
e.printStackTrace();
}

return null;
}
}

freemark 商品数据展示

1
2
3
4
5
6
7
8
9
10
11
<#list products as product>
<div class="containerFour_bottomRight_2 containerFour_bottomRight_4">
<div class="cFour_botRight_2img">
<img src="${product.imgUrl}" alt="">
</div>
<div class="cFour_botRight_2word1">
${product.name}
</div>
<div class="cFour_botRight_2word2">¥ ${product.price}</div>
</div>
</#list>

java ssm 视频教程目录