gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
Generate code for resttemplate to call graphql in spring boot
- User
Generate code for resttemplate to call graphql in spring boot
- Assistant
Sure! Here is an example of how to use RestTemplate to call a GraphQL API in Spring Boot:
import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service public class GraphQLService { private final RestTemplate restTemplate; public GraphQLService(RestTemplate restTemplate) { this.restTemplate = restTemplate; } public ResponseEntity<String> executeGraphQLQuery(String graphqlEndpoint, String query) { // Set headers HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); // Set request body String requestBody = "{\"query\":\"" + query + "\"}"; HttpEntity<String> request = new HttpEntity<>(requestBody, headers); // Send POST request to GraphQL endpoint ResponseEntity<String> response = restTemplate.exchange(graphqlEndpoint, HttpMethod.POST, request, String.class); return response; } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.