java 一对多API不通过POSTMAN工作

f87krz0w  于 2023-10-14  发布在  Java
关注(0)|答案(1)|浏览(102)

如果我通过我的代码输入数据,那么它可以完美地工作,因为我可以在PostgreSQL中看到我的父数据库和子数据库中的数据,但是当我通过Postman这样做时,子数据库没有被读取。

package com.example.project.relation;

import java.util.HashSet;
import java.util.Set;

import com.fasterxml.jackson.annotation.JsonIgnore;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.SequenceGenerator;
import jakarta.persistence.Table;

@Entity
@Table(name = "user_data")
public class User {

    @Id
    @SequenceGenerator(
            name = "student_sequence",
            sequenceName = "student_sequence",
            allocationSize = 1
    )
    @GeneratedValue(
            strategy = GenerationType.SEQUENCE,
            generator = "student_sequence"
    )
    private Long id;
    private String organization_name;
    private String user_name;
    private String password;
    private Long phone_num;
    private String email;
    private Long registration_num;
    
    @JsonIgnore
    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Set<IPaddress> ipaddress = new HashSet<>();

    public User() {

    }

    public User(Long id, String org_name, String user_name, String password, Long phone_num, String email, Long registration_num, Set<IPaddress> ipaddress) {
        this.id = id;
        this.organization_name = org_name;
        this.user_name = user_name;
        this.password = password;
        this.phone_num = phone_num;
        this.email = email;
        this.registration_num = registration_num;
        this.ipaddress = ipaddress;
    }

    public User(String organization_name, String user_name, String password, Long phone_num, String email,
                Long registration_num, Set<IPaddress> ipaddress) {
        this.organization_name = organization_name;
        this.user_name = user_name;
        this.password = password;
        this.phone_num = phone_num;
        this.email = email;
        this.registration_num = registration_num;
        this.ipaddress = ipaddress;
    }
    

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getOrganization_name() {
        return organization_name;
    }

    public void setOrganization_name(String organization_name) {
        this.organization_name = organization_name;
    }

    public String getUser_name() {
        return user_name;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Long getPhone_num() {
        return phone_num;
    }

    public void setPhone_num(Long phone_num) {
        this.phone_num = phone_num;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Long getRegistration_num() {
        return registration_num;
    }

    public void setRegistration_num(Long registration_num) {
        this.registration_num = registration_num;
    }
    
    public Set<IPaddress> getIpaddress() {
        return ipaddress;
    }

    public void setIpaddress(Set<IPaddress> ipaddress) {
        this.ipaddress = ipaddress;
    }
    
    @Override
    public String toString() {
        return "User [id=" + id +
                ", organization_name=" + organization_name +
                ", user_name=" + user_name +
                ", password=" + password +
                ", phone_num=" + phone_num +
                ", email=" + email +
                ", registration_num=" + registration_num +
                ", ipaddress=" + ipaddress + "]";
    }
}
'''

'''java  
package com.example.project.relation;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.SequenceGenerator;
import jakarta.persistence.Table;

@Entity
@Table(name = "ipaddress")
public class IPaddress {

    @Id
    @SequenceGenerator(
            name = "IP_sequence",
            sequenceName = "IP_sequence",
            allocationSize = 1
    )
    @GeneratedValue(
            strategy = GenerationType.SEQUENCE,
            generator = "IP_sequence"
    )

    private Long id;
    private String IP_address;

    @ManyToOne
    @JoinColumn(name = "user_id")
    public User user;

    public IPaddress() {

    }

    public IPaddress(Long id, String iP_address) {
        this.id = id;
        this.IP_address = iP_address;
    }

    public IPaddress(String iP_address) {
        this.IP_address = iP_address;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getIP_address() {
        return IP_address;
    }

    public void setIP_address(String iP_address) {
        IP_address = iP_address;
    }
}
package com.example.project.dto;

import com.example.project.relation.User;

public class DataRequest {
    
    private User user;

    public DataRequest() 
    {
        
    }

    public DataRequest(User user) 
    {
        this.user = user;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public String toString() {
        return "DataRequest [user=" + user + "]";
    }
    
    
}
package com.example.project.controller;

import org.springframework.web.bind.annotation.*;

import com.example.project.relation.User;
import com.example.project.service.DataService;
import java.util.List;

@RestController
@RequestMapping("/users")
public class DataController {

    private final DataService dataService;

    public DataController(DataService dataService) {
        this.dataService = dataService;
    }

    @GetMapping
    public List<User> getAllUsers() {
        return dataService.getAllUsers();

    }

    @PostMapping
    public User createUser(@RequestBody User user) {
        return dataService.createUser(user);
    }

    @PutMapping("/{id}")
    public User updateUser(@PathVariable Long id, @RequestBody User user) {
        return dataService.updateUser(id, user);
    }

    @DeleteMapping("/{id}")
    public void deleteUser(@PathVariable Long id) {
        dataService.deleteUser(id);
    }
}
package com.example.project.service;

import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Set;
import com.example.project.relation.IPaddress;
import com.example.project.relation.User;
import com.example.project.repository.IPaddressRepository;
import com.example.project.repository.UserRepository;

import jakarta.transaction.Transactional;

@Service
public class DataService {

    private final UserRepository userRepository;
    private final IPaddressRepository ipaddressRepository;

    
    public DataService(UserRepository userRepository, IPaddressRepository ipaddressRepository) {
        this.userRepository = userRepository;
        this.ipaddressRepository = ipaddressRepository;
    }

    public List<User> getAllUsers() {
        return userRepository.findAll();
    }

    public User createUser(User user) {
        Set<IPaddress> ipAddresses = user.getIpaddress();

        for (IPaddress ipAddress : ipAddresses) {
            ipAddress.setUser(user);
        }

        User savedUser = userRepository.save(user);

        for (IPaddress ipAddress : ipAddresses) {
            ipaddressRepository.save(ipAddress);
        }

        return savedUser;
    }

    public User updateUser(Long id, User user) {
        User existingUser = userRepository.findById(id).orElse(null);
        if (existingUser != null) {
            existingUser.setOrganization_name(user.getOrganization_name());
            existingUser.setUser_name(user.getUser_name());
            return userRepository.save(existingUser);
        }
        return null; // Handle not found scenario
    }

    @Transactional
    public void deleteUser(Long id) {
        boolean exists = userRepository.existsById(id);

        if (!exists) {
            throw new IllegalStateException("User with ID " + id + " does not exist");
        }
        
        userRepository.deleteById(id);
    }

}
package com.example.project.relation;

import java.util.HashSet;
import java.util.List;

import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.example.project.repository.IPaddressRepository;
import com.example.project.repository.UserRepository;

@Configuration
public class UserConfig {
    
    @Bean
    CommandLineRunner commandLineRunner(UserRepository userRepository, IPaddressRepository ipaddressRepository) {
        return args -> {
            User Steve = new User(
                "Brave", "Steve", "qwert12345", 9898987676L, "[email protected]", 12345678901234L, new HashSet<>());
            IPaddress ip1 = new IPaddress("192.168.1.1");
            IPaddress ip2 = new IPaddress("192.168.1.2");
            Steve.getIpaddress().add(ip1);
            Steve.getIpaddress().add(ip2);
            ip1.setUser(Steve);
            ip2.setUser(Steve);

            User Susan = new User(
                "Brave", "Susan", "abcd12345", 5432109876L, "[email protected]", 767676767671212L, new HashSet<>());
            IPaddress ip3 = new IPaddress("192.168.1.3");
            IPaddress ip4 = new IPaddress("192.168.1.4");
            IPaddress ip5 = new IPaddress("192.168.1.5");
            IPaddress ip6 = new IPaddress("192.168.1.6");
            Susan.getIpaddress().add(ip3);
            Susan.getIpaddress().add(ip4);
            Susan.getIpaddress().add(ip5);
            Susan.getIpaddress().add(ip6);
            ip3.setUser(Susan);
            ip4.setUser(Susan);
            ip5.setUser(Susan);
            ip6.setUser(Susan);

            userRepository.saveAll(List.of(Steve, Susan));
        };
    }
}
package com.example.project.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.example.project.relation.User;

@Repository
public interface UserRepository extends JpaRepository<User, Long>{

}

package com.example.project.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.example.project.relation.IPaddress;

@Repository
public interface IPaddressRepository extends JpaRepository<IPaddress, Long>{
    
}

请求的有效负载

{
    "organization_name": "Fall",
    "user_name": "Peter",
    "password": "qwert12345",
    "phone_num": 9898987676,
    "email": "[email protected]",
    "registration_num": 98765123450192,
    "ipaddress": [
        {
            "IP_address": "1.2.3.4"
        },
        {
            "IP_address": "0.2.3.4"
        }
    ]
}

对请求的答复

{
    "id": 29,
    "organization_name": "Fall",
    "user_name": "Peter",
    "password": "qwert12345",
    "phone_num": 9898987676,
    "email": "[email protected]",
    "registration_num": 98765123450192
}

让我知道如果任何细节需要另外。

k75qkfdt

k75qkfdt1#

您在User实体类中将@JsonIgnore标记为ipaddress,因此它不会显示在Postman的JSON响应中。请删除注解重试。

相关问题