终于在大神的帮助下成功配置了后台服务IDE,经过了一番研究终于写成了自己的第一个接口,而且是第一次运行就成功啦,下面上结果(虽然很简单~~~)
{
"errorCode": 0,
"body": {
"cityName": "北京"
}
}
写接口很繁琐,需要写6个类(接口),而且很容易混淆顺序,我仔细回顾了一下自己写接口的坑,总结书写顺序如下:
1 CityInfo .groovy(Bean类)
@Canonical
class CityInfo {
String cityName;
}
2 CityService (Service类)
public interface CityService {
public CityInfo getCity();
}
3 CityRepository (Repository类)
@Repository
public class CityRepository extends DaoSupport {
public List<CityInfo> getCity(){
List<Map<String,Object>> list=this.jdbcTemplate.queryForList("SELECT text FROM city WHERE ID =1");
CityInfo cityInfo=null;
List<CityInfo> cityInfoArrayList=new ArrayList<CityInfo>();
if(list==null){
return cityInfoArrayList;
}
for(Map<String,Object> map : list){
cityInfo = new CityInfo();
cityInfo.setCityName(map.get("text").toString());
cityInfoArrayList.add(cityInfo);
}
return cityInfoArrayList;
}
}
4 BaseService (BaseService类)
@Service
public class BaseService {
@Autowired
protected CityRepository cityRepository;
}
5 CityController.groovy(Controller)
@RestController
@RequestMapping("/city")
class CityController extends BaseController{
@GetMapping("")
public ResponseBody getBooks(){
return new ResponseBody(cityService.getCity());
}
@Autowired
private CityService cityService;
}
6 CityServiceImpl.groovy(Impl类)
@Service
class CityServiceImpl extends BaseService implements CityService {
@Override
CityInfo getCity() {
CityInfo cartInfo=new CityInfo();
List<CityInfo> cityInfoList=cityRepository.getCity();
return cityInfoList.get(0);
}
}
第一个接口虽然写成功了,但是更多的知识要学习:
1 List Map相互嵌套能不能熟练运用?(实际返回数据大部分是集合)
2 数据库操作语句不熟悉,很多语句(增删改查)都需要重新学习
3 get请求参数怎么加?,post,delete请求怎么写?
学习使人进步,一起努力。