A
A
artshelom2017-04-11 21:54:56
Spring
artshelom, 2017-04-11 21:54:56

How to send a spring mvc post request??

Help sending a request via Postman request itself({"readTimestamp":"54645646","meteoStationId":"4545","pressuere":"4564564"})
Error 400 - The request sent by the client was syntactically incorrect.
How to fix do not understand accepts:

@JsonView(Views.Public.class)
    @RequestMapping(params = {"meteoStationId", "readTimestamp", "temoerature", "pressuere", "windDirection", "windSpeed"},value = "/update", method = RequestMethod.PUT)
    public @ResponseBody AjaxResponseBody update(
            @RequestParam(value = "meteoStationId")@NotNull String meteoStationIdS,
            @RequestParam(value = "readTimestamp")@NotNull String readTimestampS,
            @RequestParam(value = "temoerature") String temoeraturS,
            @RequestParam(value = "pressuere") String pressuereS,
            @RequestParam(value = "windDirection") String windDirectionS,
            @RequestParam(value = "windSpeed") String windSpeedS
    ){
        AjaxResponseBody result = new AjaxResponseBody();
        Long meteoStationId = Long.parseLong(meteoStationIdS);
        Timestamp readTimestam = new Timestamp(Long.parseLong(readTimestampS));
        WeatherStation station = weatherStationsBD.getOne(meteoStationId, readTimestam);
        if (station==null){
            result.setCode("error");
            result.setMsg("error");
            return result;
        }
        if (temoeraturS!=null) {
            BigDecimal temoerature = new BigDecimal(temoeraturS);
            station.setTemoerature(temoerature);
        }
        if (windDirectionS!=null) {
            Integer windDirection = Integer.parseInt(windDirectionS);
            station.setWindDirection(windDirection);
        }
        if (pressuereS!=null) {
            Integer pressuere = Integer.parseInt(pressuereS);
            station.setPressuere(pressuere);
        }
        if (windSpeedS!=null) {
            Integer windSpeed = Integer.parseInt(windSpeedS);
            station.setWindSpeed(windSpeed);
        }

        weatherStationsBD.add(station);
        result.setCode("200");
        result.setMsg("OK");
        return result;
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2017-04-12
@artshelom

Well, first of all, your method is marked with a
..., method = RequestMethod.PUT.
Accordingly, it will only accept PUT requests.
Secondly, you send the BODY in the request, but not the parameters of the requests. But in the signature of your method, you say that the method parameters must be taken from the query string (the @RequestParam annotation tells us about this).
Thus, in order for the method to work in this form, you need to PUT the request with the parameters in the query string. The query string will look something like this:
/update?&readTimestamp=54645646&meteoStationId=4545&pressuere=4564564&etc.
If you want to send data in JSON, and receive it in a method from the request body, then it's better to create a Measurement class, for example. Put in it those fields that you now have described in the method signature. And bring the signature of the update method to the form:
I hope I explained clearly...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question