flutter数据请求

本文介绍了如何在Flutter应用中使用http库进行http请求,从Spring Boot后端获取数据库数据。首先展示了Spring Boot的配置、数据库连接以及相关依赖设置。接着详细说明了Flutter端如何导入http库,发起GET请求获取数据并显示在ListView中。此外,还阐述了如何通过POST请求将用户输入的数据发送到后端,并在成功后刷新页面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先,简单介绍一下spring boot的配置和连接数据库获取数据(在Log in with Atlassian account 案例基础上进行修改)。

spingboot目录结构:

 

SpingMVCConfig.java:

 

(由于从Intellij IDEA中复制的代码在这里粘贴,结构会乱,所以这里用代码截图)

Product.java:

 

ApiController.java:

package com.example.odata.controller;

import com.example.odata.dao.HeroesDao;
import com.example.odata.entity.Hero;
import com.example.odata.entity.Product;
import com.example.odata.repository.ProductsRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.beans.PropertyDescriptor;
import java.util.*;

@RestController
@RequestMapping("/api")
public class ApiController
{
@Autowired
private ProductsRepository prodectsRepository;

@GetMapping(value = "/Products",
		produces = "application/json; charset=UTF-8")
public List<Product> queryAllFromDB()
{
	return prodectsRepository.findAll();
}

@GetMapping(value = "/Products/{id}",
		produces = "application/json; charset=UTF-8")
public Optional<Product> queryOneFromDB(@PathVariable("id") Integer id)
{
	return prodectsRepository.findById(id);
}

@PostMapping("/Products")
public boolean insertToDB(@RequestBody Product data)
{
	prodectsRepository.save(data);
	return true;
}

@PutMapping("/Products")
public boolean updateToDB(@RequestBody Product data)
{
	prodectsRepository.save(data);
	return true;
}
@PatchMapping("/Products/{id}")
public boolean updatePartToDB(@PathVariable("id") Integer id,
@RequestBody Product data)
{
if (prodectsRepository.existsById(id))
{
Product oldData = prodectsRepository.getById(id);
data.setProductid(id);
		BeanUtils.copyProperties(data, oldData, getNullPropertyNames(data));

		prodectsRepository.save(oldData);
		return true;
	}

	return false;
}


@DeleteMapping("/Products/{id}")
public boolean deleteToDB(@PathVariable("id") Integer id)
{
	prodectsRepository.deleteById(id);
	return true;
}



@Autowired
private HeroesDao heroesDao;

@GetMapping(value = "/heroes", 
		produces = "application/json; charset=UTF-8")
public List<Hero> queryAllByDao()
{
	return heroesDao.findAll();
}

@GetMapping(value = "/heroes/{id}", 
		produces = "application/json; charset=UTF-8")
public Hero queryOneByDao(@PathVariable("id") int id)
{
	return heroesDao.findById(id);
}

@PostMapping("/heroes")
public Hero insertByDao(@RequestBody H
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值