commit
d333e6809f
@ -0,0 +1,11 @@
|
||||
package cn.teammodel.model.dto.admin.common;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class GroupDto {
|
||||
public List<String> ids;
|
||||
public String schoolId;
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
package cn.teammodel.utils;
|
||||
|
||||
import cn.teammodel.model.dto.admin.common.GroupDto;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.type.TypeFactory;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class GroupUtil {
|
||||
|
||||
private final Environment env;
|
||||
|
||||
public GroupUtil(Environment env) {
|
||||
this.env = env;
|
||||
}
|
||||
public static Map<String, Object> getGroupId(GroupDto dto, GroupUtil groupUtil, HttpServletRequest request) {
|
||||
Map<String, Object> mapper = new HashMap<>();
|
||||
String apiUrl = groupUtil.env.getProperty("ies.server-url-group");
|
||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||
// 创建HttpPost对象
|
||||
HttpPost httpPost = new HttpPost(apiUrl);
|
||||
|
||||
//添加请求头
|
||||
httpPost.setHeader("Authorization", request.getHeader("Authorization"));
|
||||
httpPost.setHeader("X-Auth-Authtoken", request.getHeader("X-Auth-Authtoken"));
|
||||
httpPost.setHeader("Accept", "application/json");
|
||||
httpPost.setHeader("Content-Type", "application/json; charset=utf-8");
|
||||
|
||||
// 设置请求体
|
||||
//JsonUtil.convertToJson(dto);
|
||||
//String requestBody = String.format("{\"school\":\"%s\"}","{\"name\":\"%s\"}","{\"periodId\":\"%s\"}", code,name,periodId);
|
||||
httpPost.setEntity(new StringEntity(JsonUtil.convertToJson(dto)));
|
||||
|
||||
// 发送请求
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
||||
// 获取响应实体
|
||||
HttpEntity entity = response.getEntity();
|
||||
|
||||
if (entity != null) {
|
||||
// 解析响应内容
|
||||
String jsonString = EntityUtils.toString(entity);
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
mapper = objectMapper.readValue(jsonString, TypeFactory.defaultInstance().constructMapType(Map.class, String.class, Object.class));
|
||||
}
|
||||
|
||||
// 检查响应状态码
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (statusCode != 200) throw new RuntimeException("Failed : HTTP error code : " + statusCode);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return mapper;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue