为什么myBody中`userId`字段为空

问题描述 投票:0回答:1

我正在开发一个 Spring Boot 应用程序,我正在尝试获取用户评分及其相关的酒店详细信息。使用

RestTemplate
从两个不同的微服务获取数据。但是,在 JSON 响应中,
userId
字段始终返回为
null

这是我的

UserServiceImpl
课程:

package com.anup.user.service.services.impl;

import com.anup.user.service.entities.Hotel;
import com.anup.user.service.entities.Rating;
import com.anup.user.service.entities.User;
import com.anup.user.service.exceptions.ResourceNotFoundException;
import com.anup.user.service.repositories.UserRepository;
import com.anup.user.service.services.UserServices;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@Service
@AllArgsConstructor
@Slf4j
public class UserServiceImpl implements UserServices {

    private UserRepository userRepository;
    @Autowired
    private RestTemplate restTemplate;

    @Override
    public User saveUser(User user) {
        // Generate Unique Id
        String randomUserId = UUID.randomUUID().toString();
        user.setUserId(randomUserId);
        return userRepository.save(user);
    }

    @Override
    public List<User> getAllUser() {
        return userRepository.findAll();
    }

    @Override
    public User getUser(String userId) {
        // Get user from database with the help of User Repository
        User user = userRepository.findById(userId)
                .orElseThrow(() -> new ResourceNotFoundException("User with given id not found on the server!! : " + userId));

        // Fetch ratings of the user from Rating Service
        String url = "http://localhost:8083/ratings/users/" + userId;
        Rating[] ratingsArray = restTemplate.getForObject(url, Rating[].class);
        log.info("Ratings: {}", (Object) ratingsArray);

        // Convert the array to a list for easier handling
        List<Rating> ratingsOfUser = ratingsArray != null ? Arrays.asList(ratingsArray) : new ArrayList<>();

        // Fetch hotel details for each rating
        if (!ratingsOfUser.isEmpty()) {
            for (Rating rating : ratingsOfUser) {
                String hotelUrl = "http://localhost:8082/hotels/" + rating.getHotelId();
                Hotel hotel = restTemplate.getForObject(hotelUrl, Hotel.class);
                rating.setHotel(hotel);
            }
        }

        user.setRatings(ratingsOfUser);

        return user;
    }
}

这是

Rating
课程:

package com.anup.user.service.entities;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Rating {
    private String ratingId;
    private String userId;
    private String hotelId;
    private int rating;
    private String feedback;
    private Hotel hotel;
}

这是

Hotel
课程:

package com.anup.user.service.entities;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Hotel {
    private String id;
    private String name;
    private String location;
    private String about;
}

当我发出获取用户详细信息的请求时,我收到以下 JSON 响应:

{
    "userId": "53f7e7a8-06a5-4292-be23-45748789b970",
    "name": "anup shukla",
    "email": "[email protected]",
    "about": "I am a professional Graphics designer and java developer too",
    "ratings": [
        {
            "ratingId": "66978c876190bd6f86ec4c28",
            "userid": null,
            "hotelId": "8211c1a7-e3f7-4ec6-bdda-2770b62881a3",
            "rating": 8,
            "feedback": "the Services of the hotel was not up to the mark but they tried thier best",
            "hotel": {
                "id": "8211c1a7-e3f7-4ec6-bdda-2770b62881a3",
                "name": "Moti Mahal",
                "location": "delhi",
                "about": "very good food service"
            }
        },
        {
            "ratingId": "66978fc26190bd6f86ec4c2b",
            "userid": null,
            "hotelId": "3659f56f-d08f-4315-b101-22b69334d8ee",
            "rating": 6,
            "feedback": "Okaok Service",
            "hotel": {
                "id": "3659f56f-d08f-4315-b101-22b69334d8ee",
                "name": "mannu maharaja",
                "location": "Sikanbdrabad By pass",
                "about": "paneer tikka and ambiance is good"
            }
        }
    ]
}

如您所见,

userid
字段是
null
数组中的
ratings
。我希望它与主用户对象中的
userId
相同。

什么可能导致

userid
字段变为
null
?如何解决此问题?任何帮助将不胜感激!

我希望得到的回应是这样的:

{
    "userId": "53f7e7a8-06a5-4292-be23-45748789b970",
    "name": "anup shukla",
    "email": "[email protected]",
    "about": "I am a professional Graphics designer and java developer too",
    "ratings": [
        {
            "ratingId": "66978c876190bd6f86ec4c28",
            "userId": "53f7e7a8-06a5-4292-be23-45748789b970",
            "hotelId": "8211c1a7-e3f7-4ec6-bdda-2770b62881a3",
            "rating": 8,
            "feedback": "the Services of the hotel was not up to the mark but they tried thier best",
            "hotel": {
                "id": "8211c1a7-e3f7-4ec6-bdda-2770b62881a3",
                "name": "Moti Mahal",
                "location": "delhi",
                "about": "very good food service"
            }
        },
        {
            "ratingId": "66978fc26190bd6f86ec4c2b",
            "userId": "53f7e7a8-06a5-4292-be23-45748789b970",
            "hotelId": "3659f56f-d08f-4315-b101-22b69334d8ee",
            "rating": 6,
            "feedback": "Okaok Service",
            "hotel": {
                "id": "3659f56f-d08f-4315-b101-22b69334d8ee",
                "name": "mannu maharaja",
                "location": "Sikanbdrabad By pass",
                "about": "paneer tikka and ambiance is good"
            }
        }
    ]
}
java spring-boot microservices resttemplate
1个回答
-2
投票

@覆盖 公共用户 getUser(String userId) {

User user = userRepository.findById(userId)
        .orElseThrow(() -> new ResourceNotFoundException("User with given id not found on the server!! : " + userId));


String url = "http://localhost:8083/ratings/users/" + userId;
Rating[] ratingsOfUser = restTemplate.getForObject(url, Rating[].class);
log.info("Ratings: {}", (Object) ratingsOfUser);

// Fetch hotel details for each rating and set the userId in each rating
if (ratingsOfUser != null) {
    for (Rating rating : ratingsOfUser) {
        rating.setUserId(userId); // Set the userId here
        String hotelUrl = "http://localhost:8082/hotels/" + rating.getHotelId();
        Hotel hotel = restTemplate.getForObject(hotelUrl, Hotel.class);
        rating.setHotel(hotel);
    }
}

user.setRatings(Arrays.asList(ratingsOfUser));

return user;

}

© www.soinside.com 2019 - 2024. All rights reserved.