Rmi UnmarshalException:错误解组返回

时间:2019-04-10 22:34:35

标签: java spring-boot rmi

我从事两个项目。一个使用Spring Boot实现微服务,另一个使用JavaSE客户端。在服务器端,我实现了使用RmiServiceExporter导出的RMI服务。客户评级我调用该服务,并且当我调用数据插入方法时一切正常,但是当我调用getter方法以检索数据时,我有一个异常,因为我一点都不了解

这是我的User类的代码

@Entity
@Table(name = "sgsp_user")
public class User implements Serializable {
    @Id
    @Column(nullable=false,updatable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(nullable = false,unique = true,name = "usr_name")
    private String username;
    @Column(nullable = false,name = "user_mdp")
    private String password;
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(
            name = "user_roles",
            joinColumns = @JoinColumn(name = "user_fk"),
            inverseJoinColumns = @JoinColumn(name = "roles_fk")
    )
    private List<Role> roles;
    private short status;
    private short activate;

    public User(String username, String password, short status, short activate) {
        this.username = username;
        this.password = password;
        this.status = status;
        this.activate = activate;
    }

    public User() {
    }

    public Long getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

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

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    public short getStatus() {
        return status;
    }

    public void setStatus(short status) {
        this.status = status;
    }

    public short getActivate() {
        return activate;
    }

    public void setActivate(short activate) {
        this.activate = activate;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return status == user.status &&
                activate == user.activate &&
                Objects.equals(id, user.id) &&
                Objects.equals(username, user.username) &&
                Objects.equals(password, user.password);
    }

    @Override
    public int hashCode() {

        return Objects.hash(id, username, password, status, activate);
    }
}

我的服务等级

@Service
public class UserService {

    @Autowired
    private UserRepository userRepo;

    public User saveUser(User user){
        return this.userRepo.save(user);
    }

    public void deleteUser(Long id){
        this.userRepo.deleteById(id);
    }
    public void deleteUser(User user){
        this.userRepo.delete(user);
    }

    public User updateUser(User user){
        return this.userRepo.save(user);
    }

    public User getUser(String username,String password){
        return this.userRepo.getUserByUsernameAndPassword(username,password);
    }

    public Optional<User> getUser(Long id){
        return userRepo.findById(id);
    }

    public long countUser(Role role){
        return this.userRepo.count();
    }

    public boolean exist(String username){
        return (this.userRepo.getUserByUsername(username) != null);
    }
}

我的远程界面

public interface UserRemote extends Remote{

    public User saveUser(User user) throws RemoteException;

    public boolean connexion(String username, String password) throws RemoteException;

    public long countUsers(Role role) throws RemoteException;

    public boolean userExist(String username) throws RemoteException;

    public Optional<User> getUser(Long id) throws RemoteException;
}

我的远程服务课程

@Component
public class UserRemoteService implements UserRemote {

    @Autowired
    private UserService userService;
    @Override
    public User saveUser(User user) throws RemoteException {
        return this.userService.saveUser(user);
    }

    @Override
    public boolean connexion(String username, String password) throws RemoteException {
        if(this.userService.getUser(username,password)!= null){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public long countUsers(Role role) throws RemoteException {
        return this.userService.countUser(role);
    }

    @Override
    public boolean userExist(String username) throws RemoteException {
        return this.userService.exist(username);
    }

    @Override
    public Optional<User> getUser(Long id) throws RemoteException {
        return userService.getUser(id);
    }
}

服务的出口类别

@Configuration
public class ServiceRMIExporter {

    private int port = 5400;

    public SimpleJaxWsServiceExporter jaxWsServiceExporter(){
        return new SimpleJaxWsServiceExporter();
    }

    @Bean
    @Autowired
    public RmiServiceExporter getUserRemoteService(UserRemote userService){
        RmiServiceExporter exporter = new RmiServiceExporter();
        exporter.setService(userService);
        exporter.setServiceName("userservice");
        exporter.setRegistryPort(port);
        return exporter;
    }
}

最终是客户

public class TestRmi {

    public static void main(String[] args) {
    try {
        UserRemote stub = (UserRemote) Naming.lookup("rmi://localhost:5400/userservice");

        Optional<User> usr = stub.getUser(1L);

        System.out.println(usr.toString());
    } catch (MalformedURLException | NotBoundException | RemoteException e) {
        e.printStackTrace();
        }       
    }
}

我使用java.policy和 java -Djava.security.manager -Djava.security.policy = C:\ Users \ Richie \ IdeaProjects \ sgsp \ src \ sgsp \ admin \ res \ java.policy

没有成功

grant
{
        permission java.net.SocketPermission "*:80-65535","connect,accept,listen,resolve";
        permission java.security.AllPermission;
        permission java.net.SocketPermission "localhost:5400-", "listen";

};

我得到的异常如下:

java.rmi.UnmarshalException: error unmarshalling return; nested exception is: 
    java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: java.util.Optional
    at sun.rmi.server.UnicastRef.invoke(Unknown Source)
    at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)
    at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
    at com.sun.proxy.$Proxy0.getUser(Unknown Source)
    at com.testRMI.TestRmi.main(TestRmi.java:19)
Caused by: java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: java.util.Optional
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at sun.rmi.server.UnicastRef.unmarshalValue(Unknown Source)
    ... 5 more
Caused by: java.io.NotSerializableException: java.util.Optional
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
    at sun.rmi.server.UnicastRef.marshalValue(UnicastRef.java:290)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:371)
    at sun.rmi.transport.Transport$1.run(Transport.java:200)
    at sun.rmi.transport.Transport$1.run(Transport.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案:我下载了jar文件并将其放置在我的客户端项目中,并且一切正常:这些文件如下:

  • hibernate-core-5.3.7.Final
  • javax.persistence-api-2.2
  • javax.transaction-api-1.3
  • jboss-logging-3.3.2.Final

感谢您的关注