Spring Boot风格的Geoserver通过Rest Api版本。

huangapple go评论78阅读模式
英文:

Spring Boot style edition in Geoserver by Rest Api

问题

I see that you're encountering issues with your code when trying to edit styles in GeoServer using the POST method. You're getting a Whitelabel error when accessing http://localhost:8010/styles/point and a 400 Bad Request error in Postman.

It seems like there might be an issue with your request URL or XML content. Here are a few things to check:

  1. Make sure that GeoServer is running and accessible at http://localhost:8080/geoserver/rest/.

  2. Verify that the styleName variable in your editStyle method is set correctly and matches the style you want to edit.

  3. Double-check the format and structure of the styleXml content that you are sending in the request body. Any XML syntax errors can cause issues.

  4. Ensure that your application has the necessary permissions to edit styles in GeoServer.

  5. Check if there are any GeoServer logs or error messages that provide more details about the issue.

If you've reviewed these aspects and are still facing issues, please provide more specific details or error messages for further assistance.

英文:

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

答案1

得分: 1

有通过REST API创建新样式的两个步骤,

> 您可以通过两种方式在服务器上创建新样式。在第一种方式中,创建分为两个步骤:首先在目录中创建样式条目,然后上传样式内容。

因此,您需要执行POST请求到http://localhost:8080/geoserver/rest/styles,请求体如下:

<style><name>roads_style</name><filename>roads.sld</filename></style>

以创建样式条目,然后执行PUT请求到http://localhost:8080/geoserver/rest/styles/roads_style,其中最后一个元素与上面请求体中的名称匹配,内容类型为application/vnd.ogc.sld+xml,请求体包含实际的SLD(XML)。如果要更改样式,只需使用新的SLD文件重复此步骤。

查看示例手册

英文:

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

答案2

得分: 0

Sure, here is the translated code:

@Component
public class GeoServerClient {

    RestTemplate rest;
    String url;

    @Autowired
    public GeoServerClient(RestTemplate rest, @Value("${geo.url}") String url) {
        this.rest = rest;
        this.url = url;
    }

    public void createNewStyle() {
        String createStyleUrl = geoserverUrl + "/rest/styles";
        String xmlBody = "<style><name>roads_style</name><filename>roads.sld</filename></style>";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_XML);

        HttpEntity<String> request = new HttpEntity<>(xmlBody, headers);
    }
}

Please note that I've replaced the HTML entities with their respective characters for better readability.

英文:

Is it something like this?

@Component
public class GeoServerClient {

RestTemplate rest;
String url;

@Autowired
public GeoServerClient(RestTemplate rest, @Value(&quot;${geo.url}&quot;) String url) {
this.rest= rest;
this.url= url;
}

public void createNewStyle() {
String createStyleUrl = geoserverUrl + &quot;/rest/styles&quot;;
String xmlBody = &quot;&lt;style&gt;&lt;name&gt;roads_style&lt;/name&gt;&lt;filename&gt;roads.sld&lt;/filename&gt;                    
&lt;/style&gt;&quot;;

HttpHeaders head= new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_XML);

HttpEntity&lt;String&gt; request= new HttpEntity&lt;&gt;(xmlBody, headers);
}
}

huangapple
  • 本文由 发表于 2023年7月18日 14:52:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76710179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定