简体   繁体   English

客户端发送的请求在语法上不正确。 春季后端

[英]The request sent by the client was syntactically incorrect. Spring Backend

I stuck at this problem with Post Request, which gives me error 400 bad request, i have checked the JSON RequestBody via okHttpInterceptor by Retrofit and Json validated correctly. 我坚持使用Post Request来解决这个问题,这给了我400错误的错误请求,我已经通过Retrofit通过okHttpInterceptor检查了JSON RequestBody,并且Json正确验证了。 I don't know what's the problem with requestBody. 我不知道requestBody有什么问题。 it works with ModelAttribute though, but i want to pass data as requestBody. 它虽然与ModelAttribute一起工作,但我想将数据作为requestBody传递。

This is my Spring Controller -> 这是我的Spring Controller->

@RequestMapping(value = "/updateProfile", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
    @ResponseBody
    public BasicResponse farmerUpdateProfile(@RequestBody UpdateFarmerRequest updateFarmerRequest, HttpServletRequest request) {
        BasicResponse response = new BasicResponse();

        // get authentication token and farmer ID from header
        String requestAuthToken = (String) request.getHeader(MOBILE_AUTH);
        String requestFarmerId = (String) request.getHeader(FARMER_ID);

        // update farmer data, if id and AuthTokens are same...
        // Validate Authentication Token
        try {
            if (!farmerMobileManager.validateAuthToken(requestFarmerId, requestAuthToken)) {
                response.setErrorCode(ErrorCode.BAD_CREDENTIALS);
                response.setResponse("Bad request error");
                return response;
            }
            farmerMobileManager.updateFarmerRequest(updateFarmerRequest, response, requestFarmerId);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            response.setErrorCode(ErrorCode.INTERNAL_SERVER_ERROR);
            response.setResponse("Error fetching prescriptions");
        }

        return response;
    }

This is my UpdateFarmerRequestClass. 这是我的UpdateFarmerRequestClass。

public class UpdateFarmerRequest {

    @JsonProperty("farmer")
    private Farmer farmer;

    public void setFarmer(Farmer farmer) {
        this.farmer = farmer;
    }

    public Farmer getFarmer() {
        return farmer;
    }
}

This is my Farmerclass -> 这是我的农民课->

public class Farmer extends User {

    private String farmerReferenceId;
    private String cropId;
    private String nomineeName;
    private NomineeRelation nomineeRelation;
    private String stateName;
    private String mandalName;
    private String villageName;
    private String houseNumber;
    private Boolean smartPhoneUser;
    private SocialStatus socialStatus;
    private EducationLevel educationLevel;
    private String aadharNumber;
    private String stateId;
    private String districtId;
    private String mandalId;
    private String villageId;
    private Boolean oneTimeDataEntered = false;
    private ArrayList<CropData> cropData;

    public String getHouseNumber() {
        return houseNumber;
    }

    public String getCropId() {
        return cropId;
    }

    public void setCropId(String cropId) {
        this.cropId = cropId;
    }

    public List<CropData> getCropData() {
        return cropData;
    }

    public void setCropData(List<CropData> cropData) {
        List<CropData> newCropData = new ArrayList<>();
        for (CropData cropDataItem : cropData) {
            Boolean didFind = false;
            for (CropData farmerCropData : this.cropData) {
                // if it matched in db, update the record.
                if (farmerCropData.getCrop().name().equals(cropDataItem.getCrop().name())) {
                    // update that you got the db record
                    didFind = true;
                    farmerCropData.setCropAcres(cropDataItem.getCropAcres());
                    farmerCropData.setCropName(cropDataItem.getCropName());
                    farmerCropData.setCropYield(cropDataItem.getCropYield());
                }
            }
            if (!didFind) {
                // if you didn't fnd the record, add to new crop data
                newCropData.add(cropDataItem);
            }
            this.cropData.addAll(newCropData);
        }
    }

    public void setHouseNumber(String houseNumber) {
        this.houseNumber = houseNumber;
    }

    public String getStateName() {
        return stateName;
    }

    public void setStateName(String stateName) {
        this.stateName = stateName;
    }

    public Farmer() {
        super();
    }

    public String getNomineeName() {
        return nomineeName;
    }

    public void setNomineeName(String nomineeName) {
        this.nomineeName = nomineeName;
    }

    public NomineeRelation getNomineeRelation() {
        return nomineeRelation;
    }

    public void setNomineeRelation(NomineeRelation nomineeRelation) {
        this.nomineeRelation = nomineeRelation;
    }

    public String getMandalName() {
        return mandalName;
    }

    public void setMandalName(String mandalName) {
        this.mandalName = mandalName;
    }

    public String getVillageName() {
        return villageName;
    }

    public void setVillageName(String villageName) {
        this.villageName = villageName;
    }

    public Boolean getSmartPhoneUser() {
        return smartPhoneUser;
    }

    public void setSmartPhoneUser(Boolean smartPhoneUser) {
        this.smartPhoneUser = smartPhoneUser;
    }

    public SocialStatus getSocialStatus() {
        return socialStatus;
    }

    public void setSocialStatus(SocialStatus socialStatus) {
        this.socialStatus = socialStatus;
    }

    public EducationLevel getEducationLevel() {
        return educationLevel;
    }

    public void setEducationLevel(EducationLevel educationLevel) {
        this.educationLevel = educationLevel;
    }

    public String getAadharNumber() {
        return aadharNumber;
    }

    public void setAadharNumber(String aadharNumber) {
        this.aadharNumber = aadharNumber;
    }

    public String getFarmerReferenceId() {
        return farmerReferenceId;
    }

    public void setFarmerReferenceId(String farmerReferenceId) {
        this.farmerReferenceId = farmerReferenceId;
    }

    public String getStateId() {

        return stateId;
    }

    public void setStateId(String stateId) {

        this.stateId = stateId;
    }

    public String getDistrictId() {
        return districtId;
    }

    public void setDistrictId(String districtId) {
        this.districtId = districtId;
    }

    public String getMandalId() {
        return mandalId;
    }

    public void setMandalId(String mandalId) {
        this.mandalId = mandalId;
    }

    public String getVillageId() {
        return villageId;
    }

    public void setVillageId(String villageId) {
        this.villageId = villageId;
    }

    public Boolean getOneTimeDataEntered() {
        return oneTimeDataEntered;
    }

    public void setOneTimeDataEntered(Boolean oneTimeDataEntered) {
        this.oneTimeDataEntered = oneTimeDataEntered;
    }

    public List<String> collectErrors() {
        List<String> errors = new ArrayList<String>();
        if (StringUtils.isEmpty(this.villageId)) {
            errors.add("villageId");
        }
        // if (StringUtils.isEmpty(this.nomineeName)) {
        // errors.add("nomineeName");
        // }
        if (StringUtils.isEmpty(this.getFirstName())) {
            errors.add("firstName");
        }
        if (StringUtils.isEmpty(this.getPhoneNumber()) || !CommonUtils.validatePhoneNumber(this.getPhoneNumber())) {
            errors.add("phoneNumber");
        }
        return errors;
    }

    public List<String> collectSignUpErrors() {
        List<String> errors = new ArrayList<String>();
        if (StringUtils.isEmpty(this.villageId)) {
            errors.add("villageId");
        }
        if (StringUtils.isEmpty(this.getFirstName())) {
            errors.add("firstName");
        }
        if (StringUtils.isEmpty(this.getPhoneNumber()) || !CommonUtils.validatePhoneNumber(this.getPhoneNumber())) {
            errors.add("phoneNumber");
        }
        return errors;
    }

    public static class Constants extends User.Constants {
        public static final String FARMER_REFERENCE_ID = "farmerReferenceId";
        public static final String AADHAR_NUMBER = "aadharNumber";
        public static final String MANDAL_ID = "mandalId";
        public static final String VILLAGE_ID = "villageId";
        public static final String FARMER_ID = "_id";
    }

}

this is my RequestBody that i am sending in POSTMAN. 这是我在POSTMAN中发送的RequestBody。

RequestBody -> RequestBody->

{
    "farmer": {
        "city": "",
        "mandalName": "",
        "stateName": "",
        "villageName": "",
        "countryCode": "+",
        "cropData": [{
                "crop": "RICE",
                "cropAcres": 1.0,
                "cropName": "RICE",
                "cropYield": 2.0
            }, {
                "crop": "PADDY",
                "cropAcres": 4.0,
                "cropName": "PADDY",
                "cropYield": 5.0
            }
        ],
        "cropId": "",
        "districtId": "",
        "farmerReferenceId": "",
        "firstName": "",
        "houseNumber": "",
        "id": "",
        "mandalId": "",
        "phoneNumber": "",
        "stateId": "",
        "villageId": ""
    }
}

The Data are not empty, but i am purposely not filling it for privacy purpose. 数据不为空,但我出于隐私目的不故意将其填充。

我已经解决了我的问题,这是命名约定问题,因为我将cropData传递为“ cropData”,而在服务器端是“ CropData”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 客户端发送的请求在语法上是不正确的。 在春天使用@RequestParam - The request sent by the client was syntactically incorrect. using @RequestParam in spring 客户端发送的请求在语法上不正确。 在@ManyToOne关系中休眠,春季 - The request sent by the client was syntactically incorrect. in @ManyToOne relation Hibernate, Spring Spring REST @Validated收到“ JBWEB000065:客户端发送的请求在语法上不正确。” - Spring REST @Validated getting a “JBWEB000065: The request sent by the client was syntactically incorrect.” 状态400客户端发送的请求在语法上不正确。 AngularJS http - Status 400 The request sent by the client was syntactically incorrect. Angularjs http HTTP状态400-客户端发送的请求在语法上不正确。 -在使用Hibernate的Spring MVC Web应用程序中 - HTTP Status 400 - The request sent by the client was syntactically incorrect. - within a Spring MVC web app using Hibernate Http状态400-客户端发送的请求在语法上不正确。 春季MVC - Http status 400 -The request sent by the client was syntactically incorrect. Spring MVC Spring形式:客户端发送的请求在语法上不正确() - Spring form : The request sent by the client was syntactically incorrect () spring mvc - 客户端发送的请求在语法上是不正确的 - spring mvc - The request sent by the client was syntactically incorrect Spring:客户端发送的请求在语法上是不正确的() - Spring: The request sent by the client was syntactically incorrect () SPRING框架:客户端发送的请求在语法上不正确 - SPRING framework: The request sent by the client was syntactically incorrect
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM