GVKun编程网logo

Elasticsearch 不支持 Content-Type 标头 [application/x-www-form-urlencoded](elasticsearch bulk)

7

如果您想了解Elasticsearch不支持Content-Type标头[application/x-www-form-urlencoded]和elasticsearchbulk的知识,那么本篇文章将

如果您想了解Elasticsearch 不支持 Content-Type 标头 [application/x-www-form-urlencoded]elasticsearch bulk的知识,那么本篇文章将是您的不二之选。我们将深入剖析Elasticsearch 不支持 Content-Type 标头 [application/x-www-form-urlencoded]的各个方面,并为您解答elasticsearch bulk的疑在这篇文章中,我们将为您介绍Elasticsearch 不支持 Content-Type 标头 [application/x-www-form-urlencoded]的相关知识,同时也会详细的解释elasticsearch bulk的运用方法,并给出实际的案例分析,希望能帮助到您!

本文目录一览:

Elasticsearch 不支持 Content-Type 标头 [application/x-www-form-urlencoded](elasticsearch bulk)

Elasticsearch 不支持 Content-Type 标头 [application/x-www-form-urlencoded](elasticsearch bulk)

我以前有 ElasticSearch 5.2,刚刚升级到 6.0。

我正在尝试按照此处的指南创建索引模板,但出现错误

Content-Type header [application/x-www-form-urlencoded] is not supported

我的查询是

curl -X PUT localhost:9200/_template/template_1 -d ''{  "index_patterns": ["te*", "bar*"],  "mappings": {    "type1": {      "properties": {        "host_name": {          "type": "keyword"        }      }    }  }}''

答案1

小编典典

要解决此问题,请添加 curl 选项-H ''Content-Type: application/json''


此错误是由于在 ElasticSearch 6.0 中引入了 严格的内容类型检查,如
本文所述

从 Elasticsearch 6.0 开始,所有包含正文的 REST 请求还必须为该正文提供正确的内容类型。

@JsonProperty无法用于Content-Type:application / x-www-form-urlencoded

@JsonProperty无法用于Content-Type:application / x-www-form-urlencoded

REST API映射到Java对象时,采用输入内容类型:application / x-www-form-urlencoded

 public class MyRequest {

    @JsonProperty("my_name")
    private String myName;

    @JsonProperty("my_phone")
    private String myPhone;

    //Getters and Setters of myName and myPhone.

    }

在表单输入请求中,我正在设置my_name和my_phone的值,但是MyRequest对象带有myName和myPhone作为空值。

我正在使用Jackson批注2.3 jar

有什么建议可能有什么问题吗?

@RequestBody MultiValueMap不支持内容类型'application / x-www-form-urlencoded; charset = UTF-8'

@RequestBody MultiValueMap不支持内容类型'application / x-www-form-urlencoded; charset = UTF-8'

如何解决@RequestBody MultiValueMap不支持内容类型''application / x-www-form-urlencoded; charset = UTF-8''?

问题在于,当我们使用application / x-www-form-urlencoded时,Spring不会将其理解为RequestBody。因此,如果要使用它,则必须删除@RequestBody批注。

然后尝试以下操作:

@RequestMapping(value = "/{email}/authenticate", method = RequestMethod.POST,
        consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, 
        produces = {MediaType.APPLICATION_ATOM_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody  Representation authenticate(@PathVariable("email") String anEmailAddress, MultiValueMap paramMap) throws Exception {
   if(paramMap == null && paramMap.get("password") == null) {
        throw new IllegalArgumentException("Password not provided");
    }
    return null;
}

解决方法

我写了下面的@Controller方法

@RequestMapping(value = "/{email}/authenticate",method = RequestMethod.POST,produces = {"application/json","application/xml"},consumes = {"application/x-www-form-urlencoded"}
    )
     public
        @ResponseBody
        Representation authenticate(@PathVariable("email") String anEmailAddress,@RequestBody MultiValueMap paramMap)
                throws Exception {


            if(paramMap == null || paramMap.get("password") == null) {
                throw new IllegalArgumentException("Password not provided");
            }
    }

请求失败并出现以下错误

{
  "timestamp": 1447911866786,"status": 415,"error": "Unsupported Media Type","exception": "org.springframework.web.HttpMediaTypeNotSupportedException","message": "Content type ''application/x-www-form-urlencoded;charset=UTF-8'' not supported","path": "/users/usermail%40gmail.com/authenticate"
}

[PS:泽西岛要友好得多,但鉴于此处的实际限制,现在无法使用它]

@RequestBody MultiValueMap不支持内容类型'application / x-www-form-urlencoded; charset= UTF-8'

@RequestBody MultiValueMap不支持内容类型'application / x-www-form-urlencoded; charset= UTF-8'

基于Spring @Controller对x-www-form-urlencoded的问的答案

我写了下面的@Controller方法

@RequestMapping(value = "/{email}/authenticate", method = RequestMethod.POST            , produces = {"application/json", "application/xml"}            ,  consumes = {"application/x-www-form-urlencoded"}    )     public        @ResponseBody        Representation authenticate(@PathVariable("email") String anEmailAddress,                                    @RequestBody MultiValueMap paramMap)                throws Exception {            if(paramMap == null || paramMap.get("password") == null) {                throw new IllegalArgumentException("Password not provided");            }    }

失败的请求因以下错误

{  "timestamp": 1447911866786,  "status": 415,  "error": "Unsupported Media Type",  "exception": "org.springframework.web.HttpMediaTypeNotSupportedException",  "message": "Content type ''application/x-www-form-urlencoded;charset=UTF-8'' not supported",  "path": "/users/usermail%40gmail.com/authenticate"}

[PS:Jersey要友好得多,但鉴于这里的实际限制,现在无法使用它]

答案1

小编典典

问题在于,当我们使用 application / x-www-form-urlencoded时
,Spring不会将其理解为RequestBody。因此,如果要使用它,则必须删除 @RequestBody 批注。

然后尝试以下操作:

@RequestMapping(value = "/{email}/authenticate", method = RequestMethod.POST,        consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,         produces = {MediaType.APPLICATION_ATOM_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})public @ResponseBody  Representation authenticate(@PathVariable("email") String anEmailAddress, MultiValueMap paramMap) throws Exception {   if(paramMap == null && paramMap.get("password") == null) {        throw new IllegalArgumentException("Password not provided");    }    return null;}

请注意,删除了注释 @RequestBody

@RequestBody接收Content-type为application/x-www-form-urlencoded字符串乱码

@RequestBody接收Content-type为application/x-www-form-urlencoded字符串乱码

当发送方发送请求content-type为application/x-www-form-urlencoded,后端采用@RequestBody String body接收字符串出现乱码时,应在方法体加入如下代码即可:

    @RequestMapping(value = "/tradeResult")
    public void tradeResult(@RequestBody String body) throws Exception {
        body = URLDecoder.decode(body, StandardCharsets.UTF_8.name());
    }

 

关于Elasticsearch 不支持 Content-Type 标头 [application/x-www-form-urlencoded]elasticsearch bulk的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于@JsonProperty无法用于Content-Type:application / x-www-form-urlencoded、@RequestBody MultiValueMap不支持内容类型'application / x-www-form-urlencoded; charset = UTF-8'、@RequestBody MultiValueMap不支持内容类型'application / x-www-form-urlencoded; charset= UTF-8'、@RequestBody接收Content-type为application/x-www-form-urlencoded字符串乱码等相关内容,可以在本站寻找。

本文标签: