0

I wish to edit and add a new style in geoserver rest api from Spring Boot.

My code gives error 500. If you know how to make it please, let me know. Of course if you have different method show it too please.

package exp;

import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
@Component
@RestController
public class GeoserverEdit {
@GetMapping("/addStyle")
public void addStyle() {
    String geoServerUrl = "http://10.56.56.66:8081/geoserver";
    String workspace = "your_workspace";
    String styleName = "your_style";
    String newStyleContent = "<StyledLayerDescriptor>...</StyledLayerDescriptor>";

    RestTemplate restTemplate = new RestTemplate();

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_XML);
    headers.setBasicAuth("geoserver", "geoserver");

    String styleUrl = geoServerUrl + "/rest/workspaces/" + workspace + "/styles/" + styleName + ".xml";
    URI updateUri = URI.create(styleUrl);

    RequestEntity<String> requestEntity = new RequestEntity<>(newStyleContent, headers, HttpMethod.PUT, updateUri);

    ResponseEntity<String> response = restTemplate.exchange(requestEntity, String.class);

    if (response.getStatusCode().is2xxSuccessful()) {
        System.out.println("Styl został zaktualizowany pomyślnie.");
    } else {
        System.out.println("Wystąpił błąd podczas aktualizacji stylu.");
    }
}

}
halfer
  • 19,824
  • 17
  • 99
  • 186
Piterek237
  • 61
  • 11

1 Answers1

1
RequestEntity<String> requestEntity = new RequestEntity<>(newStyleContent, headers, HttpMethod.PUT, updateUri);

ResponseEntity<String> response = restTemplate.exchange(requestEntity, String.class);

You are setting your Http put method and uri in your requestEntity. Please put your Http method in exchange method.

Your method will look something like this,

You can use HttpEntity instead of RequestEntity.

HttpEntity<String> requestEntity = new HttpEntity<>(newStyleContent, headers);

ResponseEntity<String> response = restTemplate.exchange(updatedUri.getPath(),HttpMethod.put,requestEntity, String.class);

Hope this helps.

Ajaj Ali
  • 86
  • 4
  • Hi thanks for answer. I edited the code. It`s look like this but I have now error URI is not absolute. What I did wrong? RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_XML); headers.setBasicAuth("geoserver", "geoserver"); String styleUrl = ...; URI updateUri = URI.create(styleUrl); HttpEntity requestEntity = new HttpEntity<>(newStyleContent, headers); ResponseEntity response = restTemplate.exchange(updateUri.getPath(), HttpMethod.PUT, requestEntity, String.class); – Piterek237 Jul 14 '23 at 06:52
  • If your url endpoint,which is `String styleUrl = geoServerUrl + "/rest/workspaces/" + workspace + "/styles/" + styleName + ".xml";` is valid and correct endpoint then you can call ``` ResponseEntity response = restTemplate.exchange(styleUrl,HttpMethod.put,requestEntity, String.class); ``` – Ajaj Ali Jul 14 '23 at 08:50
  • How can I correct check it. Cause I am new to this. – Piterek237 Jul 14 '23 at 09:17