Chciałbym wyciagnac value albo resonsPharse jak mogę to zrobic ?
Szybko. Jestes bardzo oszczedny w słowach. Napisz co masz, co chesz osiągnąć i pokaz jakis kod.
private ResponseEntity<String> printUser(String url, String shipId) {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_PDF, MediaType.APPLICATION_OCTET_STREAM));
HttpEntity<String> entity = new HttpEntity<>(headers);
HttpEntity<String> result = restTemplate.getForEntity(url + shipId, String.class);
result ......
return null;
}
var value = result.getStatusCode().value();
var resonsPharse = result.getStatusCode().getReasonPhrase();
Polecam dokumentację
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/ResponseEntity.html
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/HttpStatus.html
@KamilAdam: też tak chciałem ale mam tylko
TakMaszRacje napisał(a):
HttpEntity<String> result = restTemplate.getForEntity(url + shipId, String.class); }
restTemplate.getForEntity
powinien zwrócić ResponseEntity
a nie HttpEntity
. Zdokumentacji wynika że masz zły typ po lewej stronie https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html#getForEntity(java.lang.String,java.lang.Class,java.lang.Object...)
update: Nevermind: nie dopiłem kawy, myślałem, że chcesz to wyciągnąć z body :P
Skoro wołasz:
HttpEntity<String> result = restTemplate.getForEntity(url + shipId, String.class);
To w odpowiedzi masz obiekt HttpEntity z możliwością wyciągnięcia body jako String bo tak zadeklarowałeś po prawej i lewej stronie.
Zamiast podawać klasy String jako typ na który ma zostać zmapowane body, utwórz dto które odpowiada strukturze Twojej odpowiedzi i to dto zadeklaruj po prawej i lewej stronie:
HttpEntity<MyDto> result = restTemplate.getForEntity(url + shipId, MyDto.class);
Dzięki temu metoda getBody powinna zwrócić obiekt klasy MyDto a z tego obiektu to już chyba będziesz potrafił sobie wyciągnąć co potrzebujesz? ;-)