1

I'm creating an application to display different intresting data. I've been trying create generics REST controllers for several days. I need generic mongodb repository, which is used in generic service, which is used in generic controller :) You can see that schema on picture.

packages schema

Value.java

    @Data
    @NoArgsConstructor
    public class Value implements Serializable {

    @Id
    String id;
    String description;
    Integer value;

    public Value(int value, String description) {
        this.description = description;
        this.value = value;
     }
}

SpeedValue.java

@Data
@EqualsAndHashCode(callSuper = true)

@Document(collection = "speed_values")
public class SpeedValue extends Value {

    public SpeedValue(int value, String description) {
        super(value, description);
    }

}

CurrencyValue.java

@EqualsAndHashCode(callSuper = true)
@Data

@Document(collection = "currency_values")
public class CurrencyValue extends Value {

    public CurrencyValue(int value, String description) {
        super(value, description);
    }
}

My generic repository :

@NoRepositoryBean
public interface GenericValueRepository<T extends Value> extends MongoRepository<T, String> {
}

SpeedValueRepository.java

@Repository
public interface SpeedValueRepository extends GenericValueRepository<SpeedValue> {
}

And finally GenericControler.java

@Controller
public class GenericController<T extends Value> {

    private GenericServiceImpl<T> service;

    public GenericController(GenericServiceImpl<T> speedValueService) {
        this.service = speedValueService;
    }

    @GetMapping
    public List<T> getCurrencyValue() {
        return service.findAll();
    }

    @PostMapping
    public String create(@RequestBody T json) {

        T created = this.service.save(json);
        return "Added to DB: \n" + created.toString();
    }

and controller which extends GenericController

@RestController
@RequestMapping("api/values/speed")
public class SpeedValueControllerREST extends GenericController<SpeedValue>{

    @Autowired
    public SpeedValueControllerREST(SpeedValueService speedValueService) {
        super(speedValueService);
    }
}

This code doesn't work, I've been trying with many versions, but result was NPE or code didn't compile. I have to create many models, repositories, services like these above. Can you resolve that problem? Maybe I shoud do it in other way?

ChromeKK
  • 150
  • 3
  • 11

2 Answers2

1

Finally, I solved this problem by:

https://stackoverflow.com/a/53406444/10474557

That was exactly what i was looking for

ChromeKK
  • 150
  • 3
  • 11
0

I did not understood your code, you are talking about generic, what i am assuming here is you are trying to create generic API for getting any/generic request (possibly JSON), save into MongoDB, Should be able to fetch the data with generic search query, update the data with generic selection criteria etc..

I am not sure how @MongoRepository will help in this regards, I will explan how i have achieved this generic behavior for our MongoCrud service - Pseudo code, rest you have to learn and implement -

  1. Create connection and get the MongoClient for CRUD Operation - Docs https://api.mongodb.com/java/3.0/com/mongodb/MongoClient.html

  2. Write Controller method to accept Request Body as String variable (value would be json while making request) to save, And validate if Document document = Document.parse(payload); if a document, then Save to particular collection using the MongoClient instance.

  3. Write Controller method to accept Request Body as String variable (value would be json while making request), And validate it, and use MongoClient instance to query. Link for creating query json https://docs.mongodb.com/manual/tutorial/query-documents/

I hope this will help to reach your objective.

Swaraj
  • 345
  • 3
  • 17