springboot中用RestTemplate实现api

首先实现RestTemplate类的工具类

package com.m2m.management.utils;

import com.alibaba.fastjson.JSONObject;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.File;

public class RepoManager {
    private String baseUrl = "http://172.21.73.144:30001";
    RestTemplate template = new RestTemplate();
    public ResponseEntity addUser(String darkname){
        String url = baseUrl + "/adduser?username="+darkname;
        System.out.print(url);
        HttpHeaders headers = new HttpHeaders();
        HttpEntity request = new HttpEntity<>(headers);
        ResponseEntity responce = template.exchange(url, HttpMethod.GET, request, String.class);
        return responce;
    }

    public ResponseEntity deleteUser(String darkname){
        String url = baseUrl + "/deluser?username="+darkname;
        System.out.print(url);
        HttpHeaders headers = new HttpHeaders();
        HttpEntity request = new HttpEntity<>(headers);
        ResponseEntity responce = template.exchange(url, HttpMethod.GET, request, String.class);
        return responce;
    }

    public ResponseEntity addFileByReponame(
            File file,
            String darkname,
            String description,
            String license,
            String summary,
            String website,
            String filename){
        String url = baseUrl + "/addfilebyname";
        FileSystemResource resource = new FileSystemResource(file);
        MultiValueMap param = new LinkedMultiValueMap<>();

        param.add("image", resource);
        param.add("filename", filename);
        param.add("username", darkname);
        param.add("description", description);
        param.add("license", license);
        param.add("summary", summary);
        param.add("website", website);
        HttpEntity> httpEntity = new HttpEntity>(param);
        ResponseEntity responce = template.exchange(url, HttpMethod.POST, httpEntity, String.class);
        return responce;
    }

    public ResponseEntity deleteFileByReponame(String darkname, String filename){
        String url = baseUrl + "/delfilebyname?username"+darkname+"&filename="+ filename;
        HttpHeaders headers = new HttpHeaders();
        HttpEntity request = new HttpEntity<>(headers);
        ResponseEntity responce = template.exchange(url, HttpMethod.GET, request, String.class);
        return responce;
    }
}

然后在springboot的controller中的api中调用:

@RequestMapping(value = "/repoapps", method = RequestMethod.POST)
    public ResponseEntity createRepoApps(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "rid") long rid,
            @RequestParam(value = "description") String description,
            @RequestParam(value = "license", required = false) String license,
            @RequestParam(value = "summary", required = false) String summary,
            @RequestParam(value = "website", required = false) String website
            ){
        try{
            File convFile = new File( System.getProperty("java.io.tmpdir")+"/"+file.getOriginalFilename());
            file.transferTo(convFile);
            Map apkInfo =  GetApkInfo.readApk(convFile);
            String filename = file.getOriginalFilename();
            String pkgname = apkInfo.get("pkgname").toString();
            String versioncode = apkInfo.get("versioncode").toString();
            String versionname = apkInfo.get("versionname").toString();
            Optional oprepo = repoService.findById(rid);
            Repo repo = oprepo.get();
            RepoManager repoManager = new RepoManager();;
            ResponseEntity response = repoManager.addFileByReponame(
                    convFile,
                    repo.getDarkname(),
                    description, license,
                    summary,
                    website,
                    filename);
            if(JSONObject.parseObject(response.getBody()).getString("status").equals("success")){
                RepoApps repoApps = new RepoApps(filename, pkgname, versioncode, versionname);
                repoApps.setDescription(description);
                repoApps.setLicense(license);
                repoApps.setSummary(summary);
                repoApps.setWebsit(website);
                repoApps.setRepo(repo);
                repoAppsService.save(repoApps);
                return new ResponseEntity(Response.success(), HttpStatus.OK);
            }else{
                return new ResponseEntity(Response.error("add apk error"), HttpStatus.INTERNAL_SERVER_ERROR);
            }


        }catch (NoSuchElementException e){
            logger.error(e.getMessage());
            return new ResponseEntity(Response.error("rid is error"), HttpStatus.NOT_FOUND);
        }catch(Exception e){
            logger.error(e.getMessage());
            return new ResponseEntity(Response.error("server error"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

 

你可能感兴趣的:(java类)