Good afternoon I have a small query, the question is that I am running a small REST service that receives a request, and must return a custom response, in fact I already have the response, but this current response was based on a Stored Procedure, so to the answer I must return through projections in Spring Boot, the question is that I need a personalized JSON of response, but I don't know how to do it, what I see is that it would be with classes, but I have the doubt of how it would be combined with the interface that I use as a projection to retrieve and show the Stored Procedue response.
My Code
Main Class
package com.app.validate;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ValidateClientApp {
public static void main(String[] args) {
SpringApplication.run(ValidateClientApp.class, args);
}
}
Controller
package com.app.validate.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.app.validate.dao.ValidateClientAppRepository;
import com.app.validate.entity.ResponseVo;
import com.app.validate.request.BodyRequestComplete;
@RestController
public class ValidateClientAppController {
@Autowired
private ValidateClientAppRepository dao;
@PostMapping(value = "/ValidateClientApp",consumes = "application/json",produces="application/json")
public ResponseVo ValidateClient(@RequestBody BodyRequestComplete req) {
//System.out.println(driver.getMovil());
return dao.validarClienteBonificado(req.getValidateClient().getBodyRequest().getValidateClientRequest().getMovil());
}
}
Dao
package com.app.validate.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import com.app.validate.entity.DriverBonificados;
import com.app.validate.entity.ResponseVo;
@Repository
public interface ValidateClientAppRepository extends JpaRepository<DriverBonificados, Integer> {
@Query(nativeQuery = true,value = "call ValidacionClienteBonificado(:movil)")
ResponseVo validarClienteBonificado(@Param("movil") String pMovil);
}
ResponseVo (An interface is used to obtain the Stored Procedue response and be able to display it as Json)
package com.app.validate.entity;
public interface ResponseVo {
String getCode();
String getResult();
}
BodyRequest
package com.app.validate.request;
import com.fasterxml.jackson.annotation.JsonProperty;
public class BodyRequest {
@JsonProperty("ValidateClientRequest")
private ValidateClientRequest validateClientRequest;
public ValidateClientRequest getValidateClientRequest() {
return validateClientRequest;
}
public void setValidateClientRequest(ValidateClientRequest validateClientRequest) {
this.validateClientRequest = validateClientRequest;
}
}
BodyRequestComplete
package com.app.validate.request;
import com.fasterxml.jackson.annotation.JsonProperty;
public class BodyRequestComplete {
@JsonProperty("ValidateClient")
private ValidateClient validateClient;
public ValidateClient getValidateClient() {
return validateClient;
}
public void setValidateClient(ValidateClient validateClient) {
this.validateClient = validateClient;
}
}
ValidateClient
package com.app.validate.request;
import com.fasterxml.jackson.annotation.JsonProperty;
public class ValidateClient {
@JsonProperty("Body")
private BodyRequest bodyRequest;
public BodyRequest getBodyRequest() {
return bodyRequest;
}
public void setBodyRequest(BodyRequest bodyRequest) {
this.bodyRequest = bodyRequest;
}
}
ValidateClientRequest
package com.app.validate.request;
import com.fasterxml.jackson.annotation.JsonProperty;
public class ValidateClientRequest {
@JsonProperty("msisdn")
private String movil;
public String getMovil() {
return movil;
}
public void setMovil(String movil) {
this.movil = movil;
}
}
My request
{
"ValidateClient": {
"Body": {
"ValidateClientRequest": {
"msisdn": "04242125890"
}
}
}
}
My Response (current)
{
"result": "Cliente aplica para bono",
"code": "00000"
}
Desired Response
{ "ValidateClient": {
"Body":
{
"detail": {
"Code": "00000",
"Message": "Cliente aplica para bono",
}
}
}
}
Postman test
Structure of my code
Simplest way is to create another POJO with desired output and return that from service.
Change your service like below
@PostMapping(value = "/ValidateClientApp",consumes = "application/json",produces="application/json")
public Object ValidateClient(@RequestBody BodyRequestComplete req) {
//System.out.println(driver.getMovil());
ResponseVo vo = dao.validarClienteBonificado(req.getValidateClient().getBodyRequest().getValidateClientRequest().getMovil());
return MyResponse.getMyResponse(vo);
/*
//if you want to use Map solution then
return MapBasedResponse.getResponse(vo);
*/
}
public class MyResponse {
MyValidateClient ValidateClient;
public static MyResponse getMyResponse(ResponseVo vo) {
Detail detail = new Detail();
detail.setCode(vo.getCode());
detail.setMessage(vo.getResult());
MyBody body = new MyBody();
body.setDetail(detail);
MyValidateClient vc = new MyValidateClient();
vc.setBody(body);
MyResponse myresponse = new MyResponse();
myresponse.setValidateClient(vc);
return myresponse;
}
public MyValidateClient getValidateClient() {
return ValidateClient;
}
public void setValidateClient(MyValidateClient validateClient) {
ValidateClient = validateClient;
}
}
class MyValidateClient {
MyBody Body;
public MyBody getBody() {
return Body;
}
public void setBody(MyBody body) {
Body = body;
}
}
class MyBody {
Detail detail;
public Detail getDetail() {
return detail;
}
public void setDetail(Detail detail) {
this.detail = detail;
}
}
class Detail {
String code;
String Message;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMessage() {
return Message;
}
public void setMessage(String message) {
Message = message;
}
}
Other option is to use direct Json api to build Json String and return that. For example, you can create map and return map from service
public static Map<String,Object> getResponse(ResponseVo vo) {
Map<String,String> detail = new HashMap<>(2);
detail.put("code", vo.getCode());
detail.put("Message", vo.getResult());
Map<String,Object> body = new HashMap<>(1);
body.put("detail", detail);
Map<String,Object> vc = new HashMap<>(1);
body.put("Body", body);
Map<String,Object> result = new HashMap<>(1);
body.put("ValidateClient", vc);
return result;
}