Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Spring Boot geoserver editing/adding Styles

please help me 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. I appreciate your answers. Bless You all

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.");
    }
}

}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading