方式一:URL
package com.xhkjedu.controller.classin;
import com.alibaba.fastjson.JSON;
import com.xhkjedu.utils.MD5;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import com.xhkjedu.vo.classin.CResultVo;
import com.xhkjedu.vo.classin.ClassInVo;
import com.xhkjedu.vo.classin.ErrorInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
@RestController
@RequestMapping("/classin")
@Slf4j
public class ClassInController {
@PostMapping("/send_post")
public ResultVo sendPost(@RequestBody ClassInVo classin) {
String sid = "";
String secret="";
if (N_Utils.isEmpty(sid) || N_Utils.isEmpty(secret)){
return new ResultVo(1,"该学校未开通直播权限");
}
String url = classin.getUrl();
String param = classin.getParam();
MD5 md5 = new MD5();
String sign = classin.getSign();
String sign2 = md5.getJsMD5(param);
if (!sign.equals(sign2)){
return new ResultVo(1,"签名校验失败");
}
int timestamp = N_Utils.getSecondTimestamp();
String safeKey = md5.getJsMD5(secret+timestamp);
StringBuilder sb = new StringBuilder();
sb.append(param);
sb.append("&SID=").append(sid);
sb.append("&safeKey=").append(safeKey);
sb.append("&timeStamp=").append(timestamp);
String result = sendPost(url, sb.toString());
CResultVo vo = JSON.parseObject(result, CResultVo.class);
ErrorInfoVo errorInfo = vo.getError_info();
if (errorInfo.getErrno() == 1) {
return new ResultVo(0,"调用classin接口成功",result);
} else {
return new ResultVo(1,errorInfo.getError());
}
}
public static String sendPost(String url, String param) {
OutputStreamWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();
conn.setRequestProperty("accept", "application/x-www-form-urlencoded");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Accept-Charset", "UTF-8");
out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
out.write(param);
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
log.error("发送 POST 请求出现异常!" + e.getMessage());
}
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
log.error("关闭输入输出流异常", ex.getMessage());
}
}
return result;
}
}
方式二:feign
1、pom.xml引入feign包
<!--远程调用-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
2、FeignClient
package com.xhkjedu.config;
import com.xhkjedu.vo.ResultVo;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Component
@FeignClient(url = "${ssoURL}", name = "ssoURL")
public interface FeignURL {
@RequestMapping(value = "/school/ssoDeleteUser", method = RequestMethod.POST)
public ResultVo ssoDeleteUser(@RequestParam(value = "userid") Integer userid);
@RequestMapping(value = "/school/ssoupdateUser", method = RequestMethod.POST)
public ResultVo ssoUpdateUser(@RequestParam(value = "object_string") String object_string);
}
3、Controller
package com.xhkjedu.controller.system;
import com.xhkjedu.config.FeignURL;
import com.xhkjedu.service.system.UserService;
import com.xhkjedu.vo.ResultVo;
import com.xhkjedu.vo.system.TUserBean;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
private static Logger log = LoggerFactory.getLogger(UserController.class);
@Autowired
private UserService userService;
@Autowired
private FeignURL feginssoURL;
@RequestMapping(value = "/deleteTeacher", method = {RequestMethod.GET, RequestMethod.POST})
public ResultVo deleteTeacher(Integer userid) {
ResultVo result = new ResultVo();
result.setCode(1);
try {
ResultVo vo = feginssoURL.ssoDeleteUser(userid);
if (vo.getCode() == 0) {
userService.delete(userid);
result.setCode(0);
} else {
throw new Exception(vo.getMsg());
}
} catch (Exception e) {
log.error("删除用户出错:" + e.getMessage());
result.setMsg("删除用户出错:" + e.getMessage());
}
return result;
}
@RequestMapping("/modifyTeacher")
public ResultVo modifyTeacher(TUserBean user, Integer sssourcesid) {
ResultVo result = new ResultVo();
ObjectMapper mapper = new ObjectMapper();
result.setCode(1);
try {
TUserBean suser = user;
suser.setUserid(sssourcesid);
String object_string = mapper.writeValueAsString(suser);
ResultVo vo = feginssoURL.ssoUpdateUser(object_string);
if (vo.getCode() == 0) {
userService.update(user);
result.setCode(0);
} else {
throw new Exception(vo.getMsg());
}
} catch (Exception e) {
log.error("修改用户信息出错" + e.getMessage());
result.setMsg("修改用户信息出错:" + e.getMessage());
}
return result;
}
}
4、启动类加上@EnableFeignClients