JAVA网络爬虫
HttpClient

导航

 

项目结构

在这里插入图片描述

配置文件

  1. pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    		 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    
    	<groupId>com.xiaoge.demo</groupId>
    	<artifactId>http-demo</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<packaging>jar</packaging>
    
    	<name>http-demo</name>
    	<description>Demo project for Spring Boot</description>
    
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>2.0.4.RELEASE</version>
    		<relativePath/> <!-- lookup parent from repository -->
    	</parent>
    
    	<properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    		<java.version>1.8</java.version>
    	</properties>
    
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter</artifactId>
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-test</artifactId>
    			<scope>test</scope>
    		</dependency>
    
    		<dependency>
    			<groupId>org.apache.httpcomponents</groupId>
    			<artifactId>httpclient</artifactId>
    			<version>4.5.2</version>
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    	</dependencies>
    
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.springframework.boot</groupId>
    				<artifactId>spring-boot-maven-plugin</artifactId>
    			</plugin>
    		</plugins>
    	</build>
    
    
    </project>
    

实体类

  1. User

    package com.xiaoge.httpdemo.pojo;
    
    import java.io.Serializable;
    import java.util.Date;
    
    public class User implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        private Long id;
    
        // 用户名
        private String userName;
    
        // 密码
        private String password;
    
        // 姓名
        private String name;
    
        // 年龄
        private Integer age;
    
        // 性别,1男性,2女性
        private Integer sex;
    
        // 出生日期
        private Date birthday;
    
        // 创建时间
        private Date created;
    
        // 更新时间
        private Date updated;
    
        // 备注
        private String note;
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public Integer getSex() {
            return sex;
        }
    
        public void setSex(Integer sex) {
            this.sex = sex;
        }
    
        public Date getBirthday() {
            return birthday;
        }
    
        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
    
        public Date getCreated() {
            return created;
        }
    
        public void setCreated(Date created) {
            this.created = created;
        }
    
        public Date getUpdated() {
            return updated;
        }
    
        public void setUpdated(Date updated) {
            this.updated = updated;
        }
    
        public String getNote() {
            return note;
        }
    
        public void setNote(String note) {
            this.note = note;
        }
    
        @Override
        public String toString() {
            return "User [id=" + id + ", userName=" + userName + ", password="
                    + password + ", name=" + name + ", age=" + age + ", sex=" + sex
                    + ", birthday=" + birthday + ", created=" + created
                    + ", updated=" + updated + ", note=" + note + "]";
        }
    }
    
    

引导类(重点)

  1. HttpDemoApplication

    package com.xiaoge.httpdemo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;
    
    @SpringBootApplication
    public class HttpDemoApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(HttpDemoApplication.class, args);
    	}
    
    	@Bean
    	public RestTemplate restTemplate() {
    		return new RestTemplate(); // 空参数, 默认是HttpUrlConnection
    	}
    }
    
    

测试(重点)

  1. HttpDemoApplicationTests

    package com.xiaoge.httpdemo;
    
    import com.xiaoge.httpdemo.pojo.User;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    import org.springframework.web.client.RestTemplate;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = HttpDemoApplication.class)
    public class HttpDemoApplicationTests {
    
    	@Autowired
    	private RestTemplate restTemplate;
    
    	@Test
    	public void httpGet() {
    		// 第一个参数是访问地址, 第二个参数是你要拿的对象字节码
    		User user = restTemplate.getForObject("http://localhost:8088/user/1", User.class);
    		System.out.println(user);
    	}
    
    }
    
posted on 2020-05-04 19:52  gmlgxx  阅读(32)  评论(0)    收藏  举报