当前位置: 首页 > 知识库问答 >
问题:

HTTP状态400:客户端发送的请求语法不正确

司马念
2023-03-14

这里是LocationSearchMetaData.java

public class LocationSearchMetaData {
    private int page;
    private String tag;
    private SearchPreferences prefs;
    private PublishMsgSearchLocality locality;

    public PublishMsgSearchLocality getLocality() {
        return locality;
    }

    public void setLocality(PublishMsgSearchLocality locality) {
        this.locality = locality;
    }

    public SearchPreferences getPrefs() {
        return prefs;
    }

    public void setPrefs(SearchPreferences prefs) {
        this.prefs = prefs;
    }    

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }    

    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }
}

SearchPreferences.java

public abstract class SearchPreferences {
    private String radius; 
    private boolean useCurrLoc;    

    public boolean isUseCurrLoc() {
        return useCurrLoc;
    }

    public void setUseCurrLoc(boolean useCurrLoc) {
        this.useCurrLoc = useCurrLoc;
    }    

    public String getRadius() {
        return radius;
    }

    public void setRadius(String radius) {
        this.radius = radius;
    }
}

< code > publishmsgsearchlocality . Java

public class PublishMsgSearchLocality {
    private String formatted_address;
    private double lat;
    private double lon;

    public String getFormatted_address() {
        return formatted_address;
    }

    public void setFormatted_address(String formatted_address) {
        this.formatted_address = formatted_address;
    }

    public double getLat() {
        return lat;
    }

    public void setLat(double lat) {
        this.lat = lat;
    }

    public double getLon() {
        return lon;
    }

    public void setLon(double lon) {
        this.lon = lon;
    }
}

我的控制器

@RestController
@CrossOrigin
@RequestMapping("/api/v1.0/search")
public class SearchController {
    @Autowired
    private SearchService search;

    @Autowired
    private AppInstances instances;    

    @RequestMapping(value="tag",method=RequestMethod.POST)
    public List<PublishMessageSearchModel> searchByTag(@RequestBody LocationSearchMetaData metaData)
    {        
        return search.search(instances.getEsClient(), metaData.getLocality().getLat(), metaData.getLocality().getLon(), Integer.parseInt(metaData.getPrefs().getRadius()), metaData.getTag(), metaData.getPage());
    }     
}

我的客户要求是这样的

    var req = {                   
       method: 'POST',
       url: 'http://192.168.1.2:8084/mobapp/api/v1.0/search/tag',
       headers: {
         'Content-Type': "application/json"
       },
       data: searchFormService.getTagSearhReqData()
    };

    $http(req).then(function(d){
      console.log("search data :: "+JSON.stringify(d));
    }, function(e){

下面是实际数据请求

{"prefs":{"radius":"25","useCurrLoc":true},"page":1,"locality":{"formatted_address":"mylocation","lat":11.9313,"lon":60.579999999},"tag":"tag3"}

获取时出错I:

HTTP Status 400 : The request sent by the client was syntactically incorrect.

共有1个答案

孙夕
2023-03-14

尝试转义引号:\"

{\"prefs\":{\"radius\":\"25\",\"useCurrLoc\":true},\"page\":1,\"locality\":{\"formatted_address\":\"mylocation\",\"lat\":11.9313,\"lon\":60.579999999},\"tag\":\"tag3\"}

更新:您将类< code>SearchPreferences定义为抽象类。您需要删除这个修饰符,并可能添加空的构造函数(JSON mapper可能需要)。

 类似资料: