Could You please help me to edit styles in geoserver by POST method? I am in trouble with this and I am stuck in one place. When I run this code and go to http://localhost:8010/styles/point on the page and I have Whitelabel error and in postman I have an error 400 Bad Request
public class StyleRequest {
private String fill = "#042041";
private String stroke = "#040001";
public String getFill() {
return fill;
}
public void setFill(String fill) {
this.fill = fill;
}
public String getStroke() {
return stroke;
}
public void setStroke(String stroke) {
this.stroke = stroke;
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/styles")
public class StyleController {
private GeoServerStyleService styleService;
@Autowired
public StyleController(GeoServerStyleService styleService) {
this.styleService = styleService;
}
@PostMapping("/{name}")
public ResponseEntity<Void> editStyle(@PathVariable String name, @RequestBody StyleRequest styleRequest) {
styleService.editStyle(name, styleRequest.getFill(), styleRequest.getStroke());
return ResponseEntity.ok().build();
}
}
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
@Service
public class GeoServerStyleService {
private WebClient webClient;
public GeoServerStyleService() {
this.webClient = WebClient.builder()
.baseUrl("http://localhost:8080/geoserver/rest/")
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE)
.build();
}
public void editStyle(String styleName, String fill, String stroke) {
String styleXml = "<NamedLayer>\n" +
" <Name>" + styleName + "</Name>\n" +
" <UserStyle>\n" +
" <Title>A dark yellow polygon style</Title>\n" +
" <FeatureTypeStyle>\n" +
" <Rule>\n" +
" <Title>dark yellow polygon</Title>\n" +
" <PolygonSymbolizer>\n" +
" <Fill>\n" +
" <CssParameter name=\"fill\">" + fill + "</CssParameter>\n" +
" </Fill>\n" +
" <Stroke>\n" +
" <CssParameter name=\"stroke\">" + stroke + "</CssParameter>\n" +
" <CssParameter name=\"stroke-width\">0.5</CssParameter>\n" +
" </Stroke>\n" +
" </PolygonSymbolizer>\n" +
" </Rule>\n" +
" </FeatureTypeStyle>\n" +
" </UserStyle>\n" +
" </NamedLayer>";
webClient.post()
.uri("styles/" + styleName + ".xml")
.body(BodyInserters.fromValue(styleXml))
.retrieve()
.toBodilessEntity()
.block();
}
}
I tried run code on http://localhost:8010/styles/point on the page and I have Whitelabel error and in postman I have an error 400 Bad Request. I don`t know what I can try
There are two steps to creating a new style via the REST API,
You can create a new style on the server in two ways. In the first way, the creation is done in two steps: the style entry is created in the catalog, and then the style content is uploaded
So you need to do a POST to http://localhost:8080/geoserver/rest/styles
with a body of:
<style><name>roads_style</name><filename>roads.sld</filename></style>
to create the style entry, and then a PUT to http://localhost:8080/geoserver/rest/styles/roads_style
- where the last element matches the name in the body above, with a content-type of application/vnd.ogc.sld+xml
and a body containing the actual SLD (XML). If you want to change the style then you just repeat this step with the new SLD file.
See the manual for examples