habuma / spring-in-action-5-samples

Home for example code from Spring in Action 5.
Apache License 2.0
1.21k stars 1.04k forks source link

[Ch 07] Exception while using PUT method #79

Open rnaveen93 opened 4 years ago

rnaveen93 commented 4 years ago

I tried to modify an ingredient using RestTemplate PUT but received below error.

No accessor to set property private final java.lang.String tacos.Ingredient.name!"},"message":"Could not read payload!

Full stacktrace:

21:24:03.608 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP PUT http://localhost:8080/api/ingredients/FLTO 21:24:03.641 [main] DEBUG org.springframework.web.client.RestTemplate - Writing [Ingredient(id=FLTO, name=Indian Papad, type=WRAP)] with org.springframework.http.converter.json.MappingJackson2HttpMessageConverter 21:24:03.717 [main] DEBUG org.springframework.web.client.RestTemplate - Response 400 BAD_REQUEST Exception in thread "main" org.springframework.web.client.HttpClientErrorException$BadRequest: 400 : [{"cause":{"cause":{"cause":null,"message":"No accessor to set property private final java.lang.String tacos.Ingredient.name!"},"message":"Could not read payload!; nested exception is java.lang.Unsuppo... (651 bytes)] at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:101) at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:170) at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:112) at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:782) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:740) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:674) at org.springframework.web.client.RestTemplate.put(RestTemplate.java:478) at tacos.web.consumeApi.RestTemplateDemo.updateIngredient(RestTemplateDemo.java:53) at tacos.web.consumeApi.RestTemplateDemo.main(RestTemplateDemo.java:26)

Ingredient.java

@Data
@RequiredArgsConstructor
@NoArgsConstructor(access = AccessLevel.PRIVATE, force = true)
@Entity
public class Ingredient {

    @Id
    private final String id;
    private final String name;

    @Enumerated(EnumType.STRING)
    private final Type type;

    public static enum Type {
        WRAP, PROTEIN, VEGGIES, CHEESE, SAUCE
    }
}

Put RestTemplate:

Ingredient ingredient = new Ingredient("FLTO", "Indian Papad", Type.WRAP);
demo.updateIngredient(ingredient);

public void updateIngredient(Ingredient ingredient) {
        RestTemplate rest = new RestTemplate();
        rest.put("http://localhost:8080/api/ingredients/{id}", ingredient, ingredient.getId());
}
rnaveen93 commented 4 years ago

Changing Ingredient class to below resolves the issue. I removed final keyword on the fields and added a constructor. But i believe it kinda defeats the purpose (Encapsulation and or Immutability). Can someone advise.

@Data
@NoArgsConstructor
@Entity
public class Ingredient {

    @Id
    private  String id;
    private  String name;

    @Enumerated(EnumType.STRING)
    private  Type type;

    public static enum Type {
        WRAP, PROTEIN, VEGGIES, CHEESE, SAUCE
    }

    public Ingredient(String id, String name, Type type) {
        super();
        this.id = id;
        this.name = name;
        this.type = type;
    }
}
Sammygracezhu commented 4 years ago

What IDE are you using? NetBeans or Ecllipse? Are are using Ecllipe? The @Data annotation does not work for Ecllipse sometime.

On Wed, Apr 29, 2020 at 12:14 PM Naveen notifications@github.com wrote:

Changing Ingredient class to below resolves the issue. I removed final keyword on the fields and added a constructor. But i believe it kinda defeats the purpose (Encapsulation and or Immutability). Can someone advise.

@Data @NoArgsConstructor @Entity public class Ingredient {

@Id private String id; private String name;

@Enumerated(EnumType.STRING) private Type type;

public static enum Type { WRAP, PROTEIN, VEGGIES, CHEESE, SAUCE }

public Ingredient(String id, String name, Type type) { super(); this.id = id; this.name = name; this.type = type; } }

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/habuma/spring-in-action-5-samples/issues/79#issuecomment-621313332, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEUCCLLAKVFHLT437MQWFGLRPBG6BANCNFSM4MT3JL4A .