I try to build a CLI client in maven by using spring boot to consume my rest api but I have difficulties to consume it.
When I send a request they print something about jackson (see below)
I don't understand the message error at all.
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 ;
}
}
I use the client with this command :
java -jar target/rest_client-0.0.1-SNAPSHOT.jar --app.action=readOnePrisoner --app.firstName=Alan
and I got this error:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
]
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:164) ~[jackson-databind-2.11.4.jar!/:2.11.4]
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:290) ~[jackson-databind-2.11.4.jar!/:
2.11.4]
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:249) ~[jackson-databind-2.11.4.jar!/:
2.11.4]
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:26) ~[jackson-databind-2.11.4.jar!/:2
.11.4]
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129) ~[jackson-databind-2.11.4.jar!/:2.11.4]
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:371) ~[jackson-databind-2.11.4.jar!/:2.11.4
]
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:164) ~[jackson-databind-2.11.4.jar!/:2.11.4]
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129) ~[jackson-databind-2.11.4.jar!/:2.11.4]
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:371) ~[jackson-databind-2.11.4.jar!/:2.11.4
]
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:164) ~[jackson-databind-2.11.4.jar!/:2.11.4]
Looks like an attempt to deserialise some json is failing...
This line from you main application:
...calls:
It seems likely the call to get the
PrisonerEntityis returning{}or nothing at all.If you can put a breakpoint on these two lines and run them through the expression evaluator in IntelliJ Idea (
Alt+F8, usually)...Theres also often an autowired deserialisation class for the
PrisonerEntitysomewhere .. likely named something likePrisonerEntityDeserialiser(or similar) you may be able to set a break point in there and evaluate what's being returned before jackson attempts to convert it from a json (String) to an object, so you may be able to debug the issue better there.