Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot serialize class - not found: com.example.userservice.dto.UserCreatedPayload error #726

Open
huseyingoztok opened this issue Sep 16, 2024 · 0 comments

Comments

@huseyingoztok
Copy link

Even though my UserCreatedPayload class implements Serializable, I am receiving the error mentioned in the title. I would appreciate your guidance on the cause of this error.

I receive the error after the line outbox.schedule(getClass()).sendCreatedUser(payload); is processed.

Example codes

`package com.example.userservice.service;

import com.example.userservice.config.kafka.producer.KafkaProducer;
import com.example.userservice.config.kafka.properties.UserCreatedTopicProperties;
import com.example.userservice.dto.UserCreateRequest;
import com.example.userservice.dto.UserCreatedPayload;
import com.example.userservice.entity.User;
import com.example.userservice.repository.UserRepository;
import com.gruelbox.transactionoutbox.TransactionOutbox;
import lombok.RequiredArgsConstructor;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import static org.springframework.kafka.support.KafkaHeaders.*;

@service
@requiredargsconstructor
public class UserService {

private final UserRepository userRepository;

private final KafkaProducer kafkaProducer;

private final UserCreatedTopicProperties userCreatedTopicProperties;
private final TransactionOutbox outbox;

@Transactional
public User create(UserCreateRequest userCreateRequest) {
    User user = User.getUser(userCreateRequest);
    User savedUser = userRepository.save(user);

    UserCreatedPayload payload = UserCreatedPayload.GetUserCreatedPayload(savedUser, userCreateRequest.getAddressText());
    outbox.schedule(getClass()).sendCreatedUser(payload);
    return savedUser;
}

public User getUserById(Long id) {
    Optional<User> userOptional = userRepository.findById(id);
    if (!userOptional.isPresent()) {
        return null;
    }
    return userOptional.get();
}

public void sendCreatedUser(UserCreatedPayload payload) {
    Map<String, Object> headers = new HashMap<>();
    headers.put(TOPIC, userCreatedTopicProperties.getTopicName());
    headers.put(MESSAGE_KEY, payload.getId().toString());

    kafkaProducer.sendMessage(new GenericMessage<>(payload, headers));
}

}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant