0

I have a spring boot application which is making request to GeoServer to get a layer as a vector tile instead of the front end application making the request directly. The problem is I am not getting the response as a .pbf file instead I am getting it as a file. My code is below:

#controller

@ResponseBody
@GetMapping(value = "/tile-layer",produces= {"application/x-protobuf"})
public Object getWmsVectorLayerMap(@RequestParam(name = "SRS", required = true) String crs,
        @RequestParam(name = "LAYERS", required = true) String layers) {
    try {
    //  logger.info("making wms request...");
        String layer = Utilities.getLayers(layers).replace(" ", "%20");
        String url = wmsTileLayerUrl + "layers=" + layer +"&srs=" + crs;
        HttpHeaders header = new HttpHeaders();
        String auth = geoServerUsername + ":" + geoServerPassword;
        byte[] authentication = auth.getBytes();
        byte[] base64Authentication = Base64Utils.encode(authentication);
        String baseCredential = new String(base64Authentication);
        header.add(HttpHeaders.AUTHORIZATION, "Basic " + baseCredential);
        header.setAccept(Collections.singletonList(MediaType.ALL));
        header.setContentType(MediaType.parseMediaType("application/x-protobuf"));
        HttpEntity<String> request = new HttpEntity<String>(header);
        //restTemplate.getForEntity(url, Object.class);
        ResponseEntity<Object> rsp = restTemplate.exchange(new URI(url), HttpMethod.GET, request, Object.class);
        return rsp.getBody();       
    } catch (Exception e) { 
        logger.error("Error while making wms request because: "+e.getMessage());
        e.printStackTrace();
        return null;
    }
}

Is there anything I am doing wrong? How do I solve it please?

  • is the file an error message? – Ian Turton Mar 15 at 14:54
  • no it isnt. apparently i was doing it wrong. i have been able to solve it and it is getting the .pbf file now. The update code is as follow: – Babatunde Lawal Mar 18 at 10:50
  • HttpEntity<String> request = new HttpEntity<String>(header); RestTemplate restTemplate = new RestTemplate(); restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter()); //restTemplate.getForEntity(url, Object.class); ResponseEntity<byte[]> rsp = restTemplate.exchange(new URI(url), HttpMethod.GET, request, byte[].class); response.setHeader("Content-Disposition", "attachment;filename=layervectortile.pbf"); OutputStream output = response.getOutputStream(); output.write(rsp.getBody()); output.close(); response.getOutputStream().close(); – Babatunde Lawal Mar 18 at 10:52
  • I also change the return type to void so im writing the file to the HttpServletResponse object directly – Babatunde Lawal Mar 18 at 10:53
  • please make that into an answer and then accept it for future readers – Ian Turton Mar 18 at 11:04
1

No it isn't. Apparently I was doing it wrong. I have been able to solve it and it is getting the .pbf file now. The update code is as follows:

HttpEntity<String> request = new HttpEntity<String>(header); 
RestTemplate restTemplate = new RestTemplate(); 
restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
//restTemplate.getForEntity(url, Object.class); 
ResponseEntity<byte[]> rsp = restTemplate.exchange(new URI(url), HttpMethod.GET, request, byte[].class);
response.setHeader("Content-Disposition", attachment;filename=layervectortile.pbf"); 
OutputStream output = response.getOutputStream();
output.write(rsp.getBody()); 
output.close(); 
response.getOutputStream().close(); 

I also changed the return type to void so I'm writing the file to the HttpServletResponse object directly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.