HttpClient

image-20251121134112048

即可以在java程序中发送http请求

Get

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@SpringBootTest
public class HttpClientTest {
@Test
public void test() throws IOException {
//创建httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();

//创建请求对象
HttpGet get = new HttpGet("http://localhost:8080/user/shop/status");

//发送请求,接收响应结果
CloseableHttpResponse execute = httpClient.execute(get);

//获取服务端返回的状态码
int statusCode = execute.getStatusLine().getStatusCode();
System.out.println("服务端返回的状态码为:"+ statusCode);

HttpEntity entity = execute.getEntity();
String content = entity.getContent().toString();
System.out.println("服务端返回的内容为:"+ content);

//关闭资源
execute.close();
httpClient.close();
}
}

Post

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@Test
public void testPost() throws IOException {
//创建httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建请求对象
HttpPost post = new HttpPost("http://localhost:8080/admin/employee/login");

JSONObject json = new JSONObject();
json.put("username", "admin");
json.put("password", "admin");
StringEntity entity = new StringEntity(json.toJSONString());
//指定请求编码方式
entity.setContentEncoding("utf-8");
//数据格式
entity.setContentType("application/json");
post.setEntity(entity);

//发送请求
CloseableHttpResponse execute = httpClient.execute(post);
System.out.println("服务端返回的状态码为:"+ execute.getStatusLine().getStatusCode());

HttpEntity entity1 = execute.getEntity();
String body = EntityUtils.toString(entity1);
System.out.println("服务端返回的内容为:"+ body);

//关闭资源
execute.close();
httpClient.close();
}

微信小程序

链接:https://mp.weixin.qq.com/wxopen/waregister?action=step1

按照视频下载完软件后就是这个

image-20251121144015725

在设置中把不校验合法域名选上

image-20251121144121619

介绍

image-20251121144334639

image-20251121144520086

微信登录

controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@RestController
@Slf4j
@RequestMapping("/user/user")
public class UserController {

@Autowired
private UserService userService;

@Autowired
private JwtProperties jwtProperties;

//微信登录
@PostMapping("/login")
public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO){
log.info("微信登录,参数:{}", userLoginDTO);
User user = userService.wxLogin(userLoginDTO);

//为微信用户生成jwt令牌
Map<String, Object> claims = new HashMap<>();
claims.put(JwtClaimsConstant.USER_ID,user.getId());
String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(),claims);

UserLoginVO userLoginVO = UserLoginVO.builder()
.id(user.getId())
.openid(user.getOpenid())
.token(token)
.build();
return Result.success(userLoginVO);
}
}

service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
@Service
public class UserServiceImpl implements UserService {

public static final String WX_LOGIN = "https://api.weixin.qq.com/sns/jscode2session";

@Autowired
private WeChatProperties weChat;

@Autowired
private UserMapper userMapper;


//微信登录
@Override
public User wxLogin(UserLoginDTO userLoginDTO) {
//调用微信接口服务,获取当前微信用户的openid
Map<String, String> map = new HashMap();
map.put("appid", weChat.getAppid());
map.put("secret", weChat.getSecret());
map.put("js_code", userLoginDTO.getCode());
map.put("grant_type", "authorization_code");
//发送get请求
String json = HttpClientUtil.doGet(WX_LOGIN, map);

//获取openid
JSONObject jsonObject = JSONObject.parseObject(json);
String openid = jsonObject.getString("openid");

//判断openid是否为空,为空则抛出异常
if (openid != null) {
throw new LoginFailedException(MessageConstant.LOGIN_FAILED);
}

//查询当前用户是否为新用户
User user = userMapper.getByOpenid(openid);

//如果是新用户,自动注册
if (user == null){
user = User.builder()
.openid(openid)
.createTime(LocalDateTime.now())
.build();
userMapper.insert(user);
}

//返回用户对象
return user;
}
}

mapper

1
2
3
4
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
insert into user (openid, name, phone, sex, id_number, avatar, create_time)
values (#{openid}, #{name}, #{phone}, #{sex}, #{idNumber}, #{avatar}, #{createTime})
</insert>