3

I want to add optional query parameters using spring data mongodb.

Controller code:

@RestController
private final ActionService actionService;


@RequestMapping(value = "/action/{id}", method = RequestMethod.GET)
public ResponseEntity<List<Action>> getActionList(@PathVariable("id") long id,
            @RequestParam(value = "actionType", required = false) ActionType actionType,
            @RequestParam(value = " ", required = false) String[] params) {

        List<Action> actionList = actionService.getAction(id, actionType, params);
        return new ResponseEntity<>(actionList, HttpStatus.OK);
}

ActionServiceImpl.java

private ActionRepository actionRepository;

public List<Action> getAction(long id, ActionType type, String... var1) {
    return actionRepository.getByActionType(id, type, var1.length > 0 ? var1[0] : "", var1.length > 1 ? var1[1] : "", var1.length > 2 ? var1[2] : "");
}

ActionRepository.java

@Repository
public interface ActionRepository extends MongoRepository<Action, String> {

    @Query(value = "{ 'id' : ?0  , 'actionType' : ?1 ,  'param1' : ?2 , 'param2': ?3 , 'param3' : ?4 } ")
    List<Action> getByActionType(long id, ActionType type, String var1, String var2, String var3);
}

Note: 'id' is mandatory field and action type and params are optional. I want to get data based on 'id' whether I pass action type/params or not. Currently, i am getting null pointer exception in 'ActionServiceImpl' as I am not passing params and action Type. 'Action Type' is enumeration.

Can someone help me to change ActionRepository @Query tag so, that I can get data based on id without passing actionType or params. e.g. if I pass action type then mongo query should give result based on 'id $or actionType'.

1 Answer 1

2

You cannot achieve this using @Query. Other possible alternatives are

  1. Create two methods in Repository class. One which takes only id and other which takes id and other arguments. And in your service class, you can decide which one to call based on the data in hand. (Not Scalable)

  2. Use QueryDsl. With this you can create search criteria based on data you have dynamically. Some helpful links
    https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#core.extensions.querydsl
    http://www.baeldung.com/queries-in-spring-data-mongodb

  3. You can use Example. Here is the link for documentation.(This has some limitations)

In my personal experience using QueryDsl is the best way to tackle these cases and it can be easily extended for further changes in requirement.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @pvpkiran. Could you please show me an example how can I modify above using QueryDsl?
I suggest you read up on Querydsl with the help of those links and give it a try. If you are stuck, happy to help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.