VadiMayer
VadiMayer

Reputation: 21

I can't get the full body of the response in the postman

I can't get the full body of the response in the postman. A list comes with food for the restaurant. I assume that I have this problem because of the date, because when I put the @JsonIgnore annotation on the field private LocalDateTime updateDate in the entity, the food list is displayed completely, when I remove this annotation, only one food is displayed without a date, but with the name of the field.

without annotation:

[{"id":100010,"name":"Суп \"Жабо\"","cost":1400,"updateDate"}]

with annotation@JsonIgnore:

[{"id":100016,"description":"Второе \"Плов\"","cost":910},{"id":100012,"description":"Коктейль \"Агара\"","cost":760},{"id":100018,"description":"Фрукты \"Питахайя\"","cost":260},{"id":100013,"description":"Котлеты \"Банпулье\"","cost":1540},{"id":100011,"description":"Суп \"Жабо\"","cost":1400},{"id":100015,"description":"Суп \"Аладин\"","cost":720},{"id":100014,"description":"Салат \"Жандарм\"","cost":470},{"id":100010,"description":"Фрикадельки \"Мисьён\"","cost":1850},{"id":100017,"description":"Суп \"Анастасия\"","cost":847}]

code in class:

@Entity
@Table(name = "restaurant_dishes", uniqueConstraints = {@UniqueConstraint(columnNames = "restaurant_id", name = "restaurant_dishes_unique_id_rest")})
public class Dish extends AbstractBaseEntity {

    @Column(name = "description", nullable = false)
    @NotBlank
    @Size(min = 5, max = 90)
    private String description;

    @Column(name = "cost", nullable = false)
    @NotNull
    private int cost;

    @Column(name = "update_date", nullable = false)
    @NotNull
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
    @JsonIgnore
    private LocalDateTime updateDate;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "restaurant_id", nullable = false)
    @OnDelete(action = OnDeleteAction.CASCADE)
    @NotNull
    @JsonIgnore
    private Restaurant restaurant;

controller:

@RequestMapping(value = DishUIController.REST_URL, produces = MediaType.APPLICATION_JSON_VALUE)
public class DishUIController {
    private final Logger log = LoggerFactory.getLogger(getClass());

    public static final String REST_URL = "/users/dishes";

    private final RestaurantService restaurantService;

    private final DishService dishService;

    public DishUIController(RestaurantService restaurantService, DishService dishService) {
        this.restaurantService = restaurantService;
        this.dishService = dishService;
    }

    @GetMapping
    public List<Dish> getAll() {
        log.info("getAll");
        return dishService.getAll();
    }

is there a problem with the controller or what?

Upvotes: 0

Views: 183

Answers (1)

VadiMayer
VadiMayer

Reputation: 21

The solution was add customization JSON. Add class and MappingJackson2HttpMessageConverter in spring-mvc.

public class JacksonObjectMapper extends ObjectMapper {

private static final ObjectMapper MAPPER = new JacksonObjectMapper();

private JacksonObjectMapper() {
    registerModule(new Hibernate5Module());

    registerModule(new JavaTimeModule());
    configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

    setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
    setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);

    setSerializationInclusion(JsonInclude.Include.NON_NULL);
}

public static ObjectMapper getMapper() {
    return MAPPER;
}

}

in spring-mvc:

<mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper">
                    <bean class="topjava.quest.web.json.JacksonObjectMapper"/>
                </property>
            </bean>
        </mvc:message-converters>
</mvc:annotation-driven>

Upvotes: 1

Related Questions