0

I am having a little problem while sending data from my React app to my Spring Boot Controller, I am sending the data via a put method, but I get 400, error, and an error in eclipse pops up, so What I did is :

export const changeContratTypes = (idContrat, items, declaration) => {
    const endpoint = template(CONTRAT_TYPES_CHANGE);
    return instance // just an axios instance
      .put(endpoint({ idContrat }), { items, declaration })
      .then(values => values)
      .catch(err => err.response);
};

My endpoint constant is the url, simple is that, and I send declaration which is an integer and items which is an array of object, my object structure is :

{ 
   id: 1, // or 2, 3, ....
   isSelected: true, // or false
   title: "a String here"       
}

To get this in Spring boot I created this method in my controller :

@CrossOrigin(origins = "*")
@ApiOperation(value = "${contrat.recuperation}", notes = "${contrat.recuperation.notes}", response = ContratDetailDto.class)
@PutMapping(value="/{idContrat}/trtype")
@ApiModelProperty(example = "4000004")
@ApiResponses(value = {
        @ApiResponse(code = 200, message = "Enrigistrer Les types de contrats ") })
public ResponseEntity enrigistrerTypesDeContrat(@ApiParam(value = "${contrat.recuperation.param.id}") @PathVariable long idContrat, @RequestBody TypesConformites tcf) {
    if (log.isDebugEnabled()) {
        log.debug("appel de la méthode enrigistrerTypesDeContrat");
    }

    System.out.println("Voila "+tcf.getDeclaration());
    return ResponseEntity.ok(HttpStatus.OK);
}

This controller is well mapped and other methods in it works fine, but all methods I used are Get Methods.

What I did before that is creating a class used as a RequestBody :

@Getter @Setter
public class TypesConformites {

   private int declaration;
   private ArrayList<Item> items; 

   public TypesConformites() {
   }

} 

and Here is my Item class :

@Getter @Setter
public class Item {
   private int id;
   private String title;
   private boolean isSelected;

   public Item() {
   }
}

I get this error in Java :

Blockquote

JSON parse error: Unrecognized field "isSelected" (class com.apicil.cosy.contrat.controller.api.impl.external.Item), not marked as ignorable; nested exception is com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "isSelected" (class com.apicil.cosy.contrat.controller.api.impl.external.Item), not marked as ignorable (3 known properties: "title", "id", "selected"]) at [Source: (PushbackInputStream); line: 1, column: 66] (through reference chain: com.apicil.cosy.contrat.controller.api.impl.external.TypesConformites["items"]->java.util.ArrayList[0]->com.apicil.cosy.contrat.controller.api.impl.external.Item["isSelected"])

What's wrong with that code, Any help would be much appreciated.

5
  • 1
    The error states you are passing "isSelected" it should just be called "selected" according to your defined service. Commented Apr 18, 2019 at 17:34
  • can you rewrite this as answer so I can mark it as true .. thanks it worked Commented Apr 18, 2019 at 17:52
  • This is the weird issue i also faced . just changed the boolean variable name to "Selected" and also in the request body and classes Commented Apr 18, 2019 at 17:53
  • I changed it in Front, my field name was isSelected and now it is selected. Commented Apr 18, 2019 at 17:55
  • @TaouBen see the answer Commented Apr 18, 2019 at 17:58

1 Answer 1

2

Generally the Item is deserialised by jackson like this :-

public void setId(String firstName) {

public void setTitle(String lastName) {

public void setSelected(boolean isActive) {

To avoid this you can just changed the mapping name in the Item and request body.. or annotated your isSelected with @JsonProperty

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer, but I solved my problem by changing things in the Front not the back

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.