我的第一个SpringBoot项目 Component Autowired

SpringBoot是做微服务架构非常好的一套框架,下面来讲讲我的第一个SpringBoot项目;

1、依赖

    <properties>
        <spring.boot.version>1.3.2.RELEASE</spring.boot.version>
    </properties>
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <version>${spring.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>3.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>2.5</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring.boot.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

2、Application类,这是SpringBoot启动服务的类;

package simple;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import simple.service.ExampleOneService;

/**
 * Created by LK on 2016/4/24.
 */
@SpringBootApplication
public class SimpleApplication implements CommandLineRunner{

    @Autowired
    @Qualifier("exampleOneService")
    private ExampleOneService exampleOneService;

    public void run(String... strings) throws Exception {
        System.out.println(this.exampleOneService.getExampleOneMessage());
        if(strings.length > 0 && strings[0].equals("exitcode")){
            throw new ExitException();
        }
    }
    public static void main(String[] args) {
        SpringApplication.run(SimpleApplication.class,args);
    }
}

3、由于上面需要引用服务才可以获得内容,所以我们也得创建一个服务类;

package simple.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * Created by LK on 2016/4/24.
 */
@Component
public class ExampleOneService {
    @Value("${name:我的第一个SpringBoot}")  //name:后面的值可以在resources里面的application.properties中配置,如果不配置,那么久默认为程序里面的值
    private String name;

    public String getExampleOneMessage(){
        return "看," + name;
    }
}

4、上面说application.properties,那么我们还得创建一个这样子的文件,并配置好需要的信息;

name:我的第一个SpringBoot

5、去到
SimpleApplication 类中,右键运行main运行项目,又或者Teminal控制台执行mvn spring-boot:run可以运行项目
</pre><pre code_snippet_id="1660186" snippet_file_name="blog_20160424_7_6717521" name="code" class="java">

在Spring Boot中,我们可以通过编写一个工具类来实现对第三方HTTP接口的调用。通常,我们会使用RestTemplate来发送HTTP请求。以下是一个简单的示例: 首先,需要在Spring Boot项目中引入RestTemplate的依赖。在`pom.xml`中添加如下依赖(如果是Maven项目): ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` 接下来,创建一个工具类,比如`ThirdPartyApiCaller`,在这个类中注入RestTemplate并提供一个方法来执行HTTP请求: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.*; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; @Component public class ThirdPartyApiCaller { private final RestTemplate restTemplate; @Autowired public ThirdPartyApiCaller(RestTemplate restTemplate) { this.restTemplate = restTemplate; } // 发起GET请求的方法示例 public ResponseEntity<String> get(String url) { ResponseEntity<String> response = restTemplate.getForEntity(url, String.class); return response; } // 发起POST请求的方法示例 public ResponseEntity<String> post(String url, Object requestEntity) { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity<Object> entity = new HttpEntity<>(requestEntity, headers); ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class); return response; } // 可以根据需要添加更多方法,如PUT、DELETE等 } ``` 在这个类中,`get`方法用于发起GET请求,`post`方法用于发起POST请求。这些方法通过`RestTemplate`的相应方法实现HTTP调用。你也可以根据需要添加其他HTTP方法(如PUT、DELETE等)的调用。 使用此类时,你只需要注入`ThirdPartyApiCaller`这个bean,然后就可以调用相应的方法来进行HTTP调用。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值