0

I have a Response Class with the set of private fields. Front end developers asked me to send the response of a service in this JSON format. So far Response should in JSON format and like this

{
  "status": "SUCCESS",
  "message": {
    "code": "040",
    "description": "verified"
    },
  "qrContent": "aaa | bbb"

 }



        QrCodePaymentResponse response = new QrCodePaymentResponse();
         if (firstThree.equalsIgnoreCase(QRType.EZDYNAMIC.getDescription())) {
              axiPayQrCodePaymentService.ezCashDynamicQR(axiPayQrCodePayment,serviceContext);
              response.setStatus(RequestStatus.SUCCESS.getStatus());
              response.setMessage(----------------);
              response.setQrContent(returnValue.getQrContent);

    }

How to modify above code to send requested format? Thanks.

2
  • show the model QrCodePaymentResponse Commented Mar 10, 2019 at 15:51
  • Sir its not the model its just a Dto whic includes private String code; private String description; private String qrContent; private String message; private String status; Commented Mar 10, 2019 at 16:12

1 Answer 1

1

use the spring RestController. it's methods return the data as a JSON format

QrCodePaymentResponse.java

public class QrCodePaymentResponse{
    private String response;
    private String qrContent;
    private Message message; 

    //set getters and setters
}

Message.java

public class Message{
    private String code;
    private String description;

    //set getters and setters
}

AppController.java

@RestController
public class AppController {

    @RequestMapping(value="/get", method=RequestMethod.GET)
    public QrCodePaymentResponse getPaymentResponse(){
        QrCodePaymentResponse response = new QrCodePaymentResponse();
        Message message = new Message();
        //set values to message
        if (firstThree.equalsIgnoreCase(QRType.EZDYNAMIC.getDescription())) {
            axiPayQrCodePaymentService.ezCashDynamicQR(axiPayQrCodePayment,serviceContext);
            response.setStatus(RequestStatus.SUCCESS.getStatus());
            response.setMessage(message);
            response.setQrContent(returnValue.getQrContent);
        }
        return response;
    }
}
Sign up to request clarification or add additional context in comments.

9 Comments

Sir, I just asked how to set values which return the format of given response body?
@L.Anush set the values to the response object in a general way. RestController convert this response object to json format. please refer above edited answer
If i set values like that will it return in this format? "status": "SUCCESS", "message": { "code": "040", "description": "verified" }, "qrContent": "aaa | bbb"
change it to @RequestMapping(value="/get", method=RequestMethod.GET , produces= "application/json")
@usman no need to set a produces= "application/json" with @RestController. it return the json object. but if you work with @Controller, set a produces= "application/json" for return the data as a json format
|

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.