最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

spring boot - Return specified JSON format using Graphql and Java - Stack Overflow

programmeradmin4浏览0评论

Expected JSON response

{
    "sample-event": "test event",
    "sample-id": "123"
}

Actual JSON response

{
    "sampleEvent": "test event",
    "sampleId": "123"
}

This is the type of mutation to send JSON payload to an API for further processing

GraphQL Schema

type Mutation{
   sampleRequest( addDataInput: AddDataInput! ) : SampleResponse @hasScope(scopes: [insert])
}

input SampleRequest{
  sampleEvent : String
  sampleId : String
}

Models

@Data
@Getter
@Setter
public class SampleRequest{
  private String sampleEvent;
  private String sampleId;
}

Request.java

public SampleResponse SendPayload (SampleRequest sampleRequest){
    UriComponents uri = UriComponentsBuilder.fromUriString(properties.getUrl() + ENDPOINT_NAME).build();
    HttpEntity<SampleRequest> entity = new HttpEntity<>(sampleRequest, header);
    HttpStatusCode statusCode = restTemplate.exchange(uri.toString(), HttpMethod.POST, entity, HttpResponse.class).getStatusCode(); 
    SampleResponse response = new SampleResponse();
    response.setStatus(statusCode.toString());  
    return statusCode;
}

How to get the expected JSON response that has the key as sample-event and sample-id?

Expected JSON response

{
    "sample-event": "test event",
    "sample-id": "123"
}

Actual JSON response

{
    "sampleEvent": "test event",
    "sampleId": "123"
}

This is the type of mutation to send JSON payload to an API for further processing

GraphQL Schema

type Mutation{
   sampleRequest( addDataInput: AddDataInput! ) : SampleResponse @hasScope(scopes: [insert])
}

input SampleRequest{
  sampleEvent : String
  sampleId : String
}

Models

@Data
@Getter
@Setter
public class SampleRequest{
  private String sampleEvent;
  private String sampleId;
}

Request.java

public SampleResponse SendPayload (SampleRequest sampleRequest){
    UriComponents uri = UriComponentsBuilder.fromUriString(properties.getUrl() + ENDPOINT_NAME).build();
    HttpEntity<SampleRequest> entity = new HttpEntity<>(sampleRequest, header);
    HttpStatusCode statusCode = restTemplate.exchange(uri.toString(), HttpMethod.POST, entity, HttpResponse.class).getStatusCode(); 
    SampleResponse response = new SampleResponse();
    response.setStatus(statusCode.toString());  
    return statusCode;
}

How to get the expected JSON response that has the key as sample-event and sample-id?

Share Improve this question asked Feb 7 at 12:17 Divya MurugananthamDivya Muruganantham 517 bronze badges 2
  • Are you using Jackson for JSON serialization? – Çağatay Şen Commented Feb 7 at 12:33
  • yes, I am using Jackson. I tried using @JsonProperty annotation, still in this line of Request.java HttpEntity<SampleRequest> entity = new HttpEntity<>(sampleRequest, header); I m getting the entity as { "sampleEvent": "test event", "sampleId": "123" } Any idea pl – Divya Muruganantham Commented Feb 7 at 13:28
Add a comment  | 

1 Answer 1

Reset to default 1

If you're using Jackson (Spring Boot uses it, so I think you are) you can annotate your fields with @JsonProperty to override the generated name.

@Data
@Getter
@Setter
public class SampleRequest{
  @JsonProperty("sample-event")
  private String sampleEvent;

  @JsonProperty("sample-id")
  private String sampleId;
}

JavaDoc

Spring boot docs on Jackson

发布评论

评论列表(0)

  1. 暂无评论