简体   繁体   中英

Spring boot CLI client consuming Rest Api

I have build a rest API that works.

Now I try to buid a CLI client using spring boot but I have difficulties.

I think it's because I am not using the RestTemplate correctly?

I build a CLI client and I have errors for every request; if I want to use the delete method for exemple, I use this line:

java -jar target/rest_client-0.0.1-SNAPSHOT.jar --app.action=deletePrisoner --app.firstName=Thomas

here is the main class

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
public class RestClientApplication implements ApplicationRunner {
    @Autowired
    private PrisonerResource prisonerResource;

    public static void main(String[] args) {
        SpringApplication.run(RestClientApplication.class, args);
    }


    @Override
    public void run(ApplicationArguments args) throws Exception {
        if (args.containsOption("app.action")) {
            switch (args.getOptionValues("app.action").get(0)){
                case "readOnePrisoner":
                    try {
                        PrisonerEntity prisonerDto = prisonerResource.readOne(args.getOptionValues("app.firstName").get(0));
                        System.out.println(prisonerDto.getFirstName());
                    } catch (HttpClientErrorException e) {
                        if (e.getStatusCode()== HttpStatus.NOT_FOUND)
                            System.err.println("not found");
                    }
                    break;
                case "readAllPrisoner":
                    break;
                case "deletePrisoner":
                    try{
                        prisonerResource.delete(args.getOptionValues("app.firstName").get(0));

                    } catch (HttpClientErrorException e){
                        if (e.getStatusCode()== HttpStatus.NOT_FOUND);
                        System.err.println("not found");
                    }
                    break;
                case "updatePrisoner":
                    try{
                        prisonerResource.update(args.getOptionValues("app.firstName").get(0));

                    } catch(HttpClientErrorException e){

                    }
                    break;
                case "createPrisoner":
                    try{
                        prisonerResource.create();

                    } catch(HttpClientErrorException e){

                    }
                    break;

here is the PrisonerResource class:


@Component
public class PrisonerResource {
    private final RestTemplate restTemplate;

    @Autowired
    public PrisonerResource(RestTemplateBuilder restTemplateBuilder) {
        this.restTemplate = restTemplateBuilder.rootUri("http//localhost/api/v1/prisoners").build();
    }


    private PrisonerEntity prisoner;

    public void create(){
        PrisonerEntity response = restTemplate.postForObject("",prisoner,PrisonerEntity.class);
        return;
    }


    public PrisonerEntity readOne(String id){
        return restTemplate.getForObject("/{id}", PrisonerEntity.class, id);
    }

    public PagedModel<PrisonerEntity> readAll(int page) {
        ResponseEntity<PagedModel<PrisonerEntity>> result = restTemplate.exchange("/?page={page}",
                HttpMethod.GET,
                null, new ParameterizedTypeReference<PagedModel<PrisonerEntity>>() {},
                page);
        return result.getBody();
    }

    public void update(String id){
        restTemplate.put("/{id}", PrisonerEntity.class, id);
        return ;

    }

    public void delete(String id){
        restTemplate.delete("/{id}", PrisonerEntity.class, id);
        return ;
    }
}

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-02-19 21:17:06.773 ERROR 15116 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalStateException: Failed to execute ApplicationRunner
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:798) ~[spring-boot-2.4.2.jar!/:2.4.2]
        at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:785) ~[spring-boot-2.4.2.jar!/:2.4.2]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:333) ~[spring-boot-2.4.2.jar!/:2.4.2]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1311) ~[spring-boot-2.4.2.jar!/:2.4.2]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300) ~[spring-boot-2.4.2.jar!/:2.4.2]
        at fit.bietjv.rest_client.RestClientApplication.main(RestClientApplication.java:28) ~[classes!/:0.0.1-SNAPSHOT]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) ~[na:na]
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
        at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) ~[rest_client-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:107) ~[rest_client-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) ~[rest_client-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88) ~[rest_client-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
Caused by: java.lang.IllegalArgumentException: URI is not absolute
        at java.base/java.net.URL.fromURI(URL.java:719) ~[na:na]
        at java.base/java.net.URI.toURL(URI.java:1139) ~[na:na]
        at org.springframework.http.client.SimpleClientHttpRequestFactory.createRequest(SimpleClientHttpRequestFactory.java:145) ~[spring-web-5.3.3.jar!
/:5.3.3]
        at org.springframework.http.client.support.HttpAccessor.createRequest(HttpAccessor.java:124) ~[spring-web-5.3.3.jar!/:5.3.3]
        at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:771) ~[spring-web-5.3.3.jar!/:5.3.3]
        at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:710) ~[spring-web-5.3.3.jar!/:5.3.3]
        at org.springframework.web.client.RestTemplate.delete(RestTemplate.java:554) ~[spring-web-5.3.3.jar!/:5.3.3]
        at fit.bietjv.rest_client.PrisonerResource.delete(PrisonerResource.java:50) ~[classes!/:0.0.1-SNAPSHOT]
        at fit.bietjv.rest_client.RestClientApplication.run(RestClientApplication.java:49) ~[classes!/:0.0.1-SNAPSHOT]
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:795) ~[spring-boot-2.4.2.jar!/:2.4.2]
        ... 13 common frames omitted

your URI is malformed here: "http//localhost/api/v1/prisoners" you are missing a : for a correct URI: "http://localhost/api/v1/prisoners" .

oh thank you that was indeed the problem but now I got this error:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-02-20 16:46:06.495 ERROR 3960 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalStateException: Failed to execute ApplicationRunner
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:798) ~[spring-boot-2.4.2.jar!/:2.4.2]
        at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:785) ~[spring-boot-2.4.2.jar!/:2.4.2]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:333) ~[spring-boot-2.4.2.jar!/:2.4.2]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1311) ~[spring-boot-2.4.2.jar!/:2.4.2]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300) ~[spring-boot-2.4.2.jar!/:2.4.2]
        at fit.bietjv.rest_client.RestClientApplication.main(RestClientApplication.java:28) ~[classes!/:0.0.1-SNAPSHOT]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) ~[na:na]
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
        at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) ~[rest_client-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:107) ~[rest_client-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) ~[rest_client-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88) ~[rest_client-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost/api/v1/prisoners/Thomas": Connection r
efused: connect; nested exception is java.net.ConnectException: Connection refused: connect
        at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:784) ~[spring-web-5.3.3.jar!/:5.3.3]
        at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:710) ~[spring-web-5.3.3.jar!/:5.3.3]
        at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:333) ~[spring-web-5.3.3.jar!/:5.3.3]
        at fit.bietjv.rest_client.PrisonerResource.readOne(PrisonerResource.java:32) ~[classes!/:0.0.1-SNAPSHOT]
        at fit.bietjv.rest_client.RestClientApplication.run(RestClientApplication.java:38) ~[classes!/:0.0.1-SNAPSHOT]
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:795) ~[spring-boot-2.4.2.jar!/:2.4.2]
        ... 13 common frames omitted
Caused by: java.net.ConnectException: Connection refused: connect
        at java.base/sun.nio.ch.Net.connect0(Native Method) ~[na:na]
        at java.base/sun.nio.ch.Net.connect(Net.java:574) ~[na:na]
        at java.base/sun.nio.ch.Net.connect(Net.java:563) ~[na:na]
        at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588) ~[na:na]
        at java.base/java.net.Socket.connect(Socket.java:648) ~[na:na]
        at java.base/java.net.Socket.connect(Socket.java:597) ~[na:na]
        at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:182) ~[na:na]
        at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:474) ~[na:na]
        at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:569) ~[na:na]
        at java.base/sun.net.www.http.HttpClient.<init>(HttpClient.java:242) ~[na:na]
        at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:341) ~[na:na]
        at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:362) ~[na:na]
        at java.base/sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1261) ~[na:na]
        at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1194) ~[na:na]
        at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1082) ~[na:na]
        at java.base/sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:1016) ~[na:na]
        at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:76) ~[spring-web-5.3.3
.jar!/:5.3.3]
        at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) ~[spring-web-5
.3.3.jar!/:5.3.3]
        at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:66) ~[spring-web-5.3.3.jar!/:5.3.3]
        at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:775) ~[spring-web-5.3.3.jar!/:5.3.3]
        ... 18 common frames omitted


C:\Users\melvi\rest_client1>java -jar target/rest_client-0.0.1-SNAPSHOT.jar --app.action=readOnePrisoner --app.firstName=Thomas

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.2)

2021-02-20 16:47:15.946  INFO 16888 --- [           main] f.b.rest_client.RestClientApplication    : Starting RestClientApplication v0.0.1-SNAPSHOT usin
g Java 15 on LAPTOP-ICIUHTGA with PID 16888 (C:\Users\melvi\rest_client1\target\rest_client-0.0.1-SNAPSHOT.jar started by melvi in C:\Users\melvi\rest_c
lient1)
2021-02-20 16:47:15.950  INFO 16888 --- [           main] f.b.rest_client.RestClientApplication    : No active profile set, falling back to default prof
iles: default
2021-02-20 16:47:17.334  INFO 16888 --- [           main] f.b.rest_client.RestClientApplication    : Started RestClientApplication in 2.102 seconds (JVM
 running for 2.817)
2021-02-20 16:47:19.511  INFO 16888 --- [           main] ConditionEvaluationReportLoggingListener :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-02-20 16:47:19.571 ERROR 16888 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalStateException: Failed to execute ApplicationRunner
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:798) ~[spring-boot-2.4.2.jar!/:2.4.2]
        at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:785) ~[spring-boot-2.4.2.jar!/:2.4.2]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:333) ~[spring-boot-2.4.2.jar!/:2.4.2]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1311) ~[spring-boot-2.4.2.jar!/:2.4.2]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300) ~[spring-boot-2.4.2.jar!/:2.4.2]
        at fit.bietjv.rest_client.RestClientApplication.main(RestClientApplication.java:28) ~[classes!/:0.0.1-SNAPSHOT]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) ~[na:na]
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
        at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) ~[rest_client-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:107) ~[rest_client-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) ~[rest_client-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88) ~[rest_client-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost/api/v1/prisoners/Thomas": Connection r
efused: connect; nested exception is java.net.ConnectException: Connection refused: connect
        at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:784) ~[spring-web-5.3.3.jar!/:5.3.3]
        at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:710) ~[spring-web-5.3.3.jar!/:5.3.3]
        at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:333) ~[spring-web-5.3.3.jar!/:5.3.3]
        at fit.bietjv.rest_client.PrisonerResource.readOne(PrisonerResource.java:32) ~[classes!/:0.0.1-SNAPSHOT]
        at fit.bietjv.rest_client.RestClientApplication.run(RestClientApplication.java:38) ~[classes!/:0.0.1-SNAPSHOT]
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:795) ~[spring-boot-2.4.2.jar!/:2.4.2]
        ... 13 common frames omitted
Caused by: java.net.ConnectException: Connection refused: connect
        at java.base/sun.nio.ch.Net.connect0(Native Method) ~[na:na]
        at java.base/sun.nio.ch.Net.connect(Net.java:574) ~[na:na]
        at java.base/sun.nio.ch.Net.connect(Net.java:563) ~[na:na]
        at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588) ~[na:na]
        at java.base/java.net.Socket.connect(Socket.java:648) ~[na:na]
        at java.base/java.net.Socket.connect(Socket.java:597) ~[na:na]
        at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:182) ~[na:na]
        at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:474) ~[na:na]
        at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:569) ~[na:na]
        at java.base/sun.net.www.http.HttpClient.<init>(HttpClient.java:242) ~[na:na]
        at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:341) ~[na:na]
        at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:362) ~[na:na]
        at java.base/sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1261) ~[na:na]
        at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1194) ~[na:na]
        at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1082) ~[na:na]
        at java.base/sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:1016) ~[na:na]
        at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:76) ~[spring-web-5.3.3
.jar!/:5.3.3]
        at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) ~[spring-web-5
.3.3.jar!/:5.3.3]
        at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:66) ~[spring-web-5.3.3.jar!/:5.3.3]
        at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:775) ~[spring-web-5.3.3.jar!/:5.3.3]
        ... 18 common frames omitted

It clearly means that I have trouble connecting to the api, but before sending a request, I did run the api.

Is it a problem of configuration?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM