0

I am using Spring Boot to return data from a Repository. I would like to format my JSON so that it plays nicely with ExtJS' ajax handling. Essentially I would like to include properties to handle success/failure, count, and errorMsg along with a List of data from the repository.

I have tried by creating a ResponseDTO object that I'm returning from my Rest Controller.

@RestController
public class AdminController {

    private static final Logger logger = LogManager.getLogger(AdminController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private SecurityService securityService;

    @Autowired
    private UserValidator userValidator;

    @GetMapping("/searchUsers")
    public ResponseDTO searchUsers(String name, String active) {
        int activeFlag;
        List<User> users;
        ResponseDTO resp;
        if(active.equals("true")) {
            activeFlag = 1;
        } else activeFlag=0;

        if(StringUtils.isEmpty(name)) {
            users= userService.findAllUsers(activeFlag);
        } else {
            users= userService.findByUsernameActive(name, activeFlag);
        }   

        return new ResponseDTO(users, true);
    }
}

Here's my DTO that I use in the controller:

public class ResponseDTO {
    private boolean success;
    private int count = 0;

    private List<?> values;

    public boolean getSuccess() {
        return this.success;
    }
    public void setState(boolean st) {
        this.success=st;
    }
    public int getCount() {
        return this.count;
    }
    public void setCount(int cnt) {
        this.count=cnt;
    }

    public List<?>getValues() {
        return this.values;
    }
    public void setValues(List<?> vals) {
        this.values = vals;
    }

    public ResponseDTO(List<?> items, boolean state) {
        this.success = state;
        values = items;
        this.count = items.size();
    }
}

Here's what the JSON I get back looks like:

{
  "ResponseDTO": {
                    "success":true,
                    "count":2,
                    "values":[{obj1 } , { obj2}]
                 }
}

what I would like to get is something more like:

{
   "success" : true,
   "count" : 2,
   "values" [{obj1},{obj2}]
}

I'm using Spring Boot and Jackson annotations. I have used an annotation to ignore individual fields in the objects in the results array, but I can't find a way to unwrap the ResponseDTO object to not include the class name.

5
  • Does this answer your question? Spring JSON Response: Serialize only the response object content (do not Wrap Root Value) Commented Mar 3, 2020 at 22:40
  • Do you want such a json for all POJOs or jus this one? Commented Mar 4, 2020 at 3:04
  • I want to unwrap just this POJO. Commented Mar 9, 2020 at 20:30
  • @pvlt - yes and no... I'm using Spring Boot so I'm not doing all the XML configuration of the app. Commented Mar 9, 2020 at 20:39
  • link what I present also contain answer with bean definition and ObjectMapper. maybe its ok for you Commented Mar 9, 2020 at 20:52

1 Answer 1

1

When you serialize ResponseDTO POJO, you should not get 'ResponseDTO' in the response by default. Because, the root wrap feature is disabled by default. See the doc here. If you have the below code, please remove it.

mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
Sign up to request clarification or add additional context in comments.

1 Comment

I did this from my application.properties spring.jackson.serialization.wrap-root-value=false

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.