Skip to content
Advertisement

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

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement