HttpClient

即可以在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 { 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 { 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
按照视频下载完软件后就是这个

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

介绍


微信登录
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);
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) { 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"); String json = HttpClientUtil.doGet(WX_LOGIN, map);
JSONObject jsonObject = JSONObject.parseObject(json); String openid = jsonObject.getString("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>
|