Replies: 3 comments 1 reply
-
I don't think we'll have expertise to help you. It sounds like "yes, that doesn't work." But even if there were something to be done about it, this sounds like either a Swagger or Protobuf issue. |
Beta Was this translation helpful? Give feedback.
-
This question is not related to gRPC.
|
Beta Was this translation helpful? Give feedback.
-
1. Create a DTOCreate a new DTO class that represents your // src/main/java/com/example/dto/GetTestRequestDTO.java
package com.example.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel(description = "DTO for GetTestRequest")
public class GetTestRequestDTO {
@ApiModelProperty(value = "Description of someField")
private String someField;
// Add other fields here
// Getters and setters
public String getSomeField() {
return someField;
}
public void setSomeField(String someField) {
this.someField = someField;
}
// Add other getters and setters
} 2. Update Controller to Use DTOModify your TestController to use the DTO:
// src/main/java/com/example/controller/TestController.java
package com.example.controller;
import com.example.dto.GetTestRequestDTO;
import com.example.service.TestService;
import com.google.protobuf.util.JsonFormat;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private TestService service;
@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> getTest(@RequestBody GetTestRequestDTO dto) {
try {
GetTestRequest request = GetTestRequest.newBuilder()
.setSomeField(dto.getSomeField())
// Set other fields
.build();
GetTestResponse response = service.getTest(request);
return ResponseEntity.ok(JsonFormat.printer().print(response));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
} 3. Ensure Swagger ConfigurationEnsure Swagger is configured correctly in your project. If using Springdoc OpenAPI, ensure your configuration is set up to scan for your controllers and DTOs: // src/main/java/com/example/config/OpenApiConfig.java
package com.example.config;
import org.springdoc.core.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class OpenApiConfig {
@Bean
public GroupedOpenApi publicApi() {
return GroupedOpenApi.builder()
.group("public")
.pathsToMatch("/**")
.build();
}
} |
Beta Was this translation helpful? Give feedback.
-
Use Case :
We are using gRPC in our microservices setup, where gRPC is utilized for internal communication, while REST APIs are employed for external interactions. Additionally, we are using the same POJOs generated after proto file compilation.
Controller is looking like
With this code, we are facing an issue in Swagger documentation. As the GetTestRequest is generated from the proto file and is a public static final class type, we cannot use them directly as a request body.
Thanks is advance.
Beta Was this translation helpful? Give feedback.
All reactions