当前位置:AIGC资讯 > AIGC > 正文

AI绘画

AI 绘画 智能文生图-API 文档-文档中心-腾讯云

使用HTTP请求直接调用

然后,在你的Controller类中,你可以使用Spring的RestTemplate类来发送HTTP请求。以下是一个简单的示例:

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class PaintController {
    @PostMapping("/api/paint/create")
    public ResponseEntity<String> createPainting(@RequestBody String inputText) {
        RestTemplate restTemplate = new RestTemplate();
        String apiUrl = "https://aiart.tencentcloudapi.com/";
        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Type", "application/json");
        HttpEntity<String> entity = new HttpEntity<>(inputText, headers);
        ResponseEntity<String> response = restTemplate.exchange(apiUrl, HttpMethod.POST, entity, String.class);
        return ResponseEntity.ok(response.getBody());
    }
}

在上面的示例中,我们使用类RestTemplate发送POST请求到腾讯云API URL,并将请求体和头部信息设置好。然后,我们处理返回的响应,并将结果返回给调用者。

普通接口示例

AppConfig

package com.spring.config;

import com.spring.component.RestComponent;
import com.spring.service.DrawService;
import com.spring.service.impl.DrawServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "com.spring.component")
public class AppConfig {
    @Bean
    public DrawService drawingService() {
        return new DrawServiceImpl();
    }
    @Bean
    public RestComponent restComponent(DrawService drawService) {
        return new RestComponent(drawService);
    }
}

RestComponent

package com.spring.component;

import com.spring.service.DrawService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/draw")
public class RestComponent {
    private final DrawService drawService;
    @Autowired
    public RestComponent(DrawService drawService) {
        this.drawService = drawService;
    }
    @PostMapping("/ecommerce")
    public void drawECommerceDesign(@RequestBody String designContent) {
        drawService.drawECommerceDesign(designContent);
    }
    @PostMapping("/anime")
    public void drawAnimePoster(@RequestBody String posterContent) {
        drawService.drawAnimePoster(posterContent);
    }
    @PostMapping("/business")
    public void drawBusinessPoster(@RequestBody String posterContent) {
        drawService.drawBusinessPoster(posterContent);
    }
}

DrawController

package com.spring.controller;

import com.spring.service.DrawService;
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;

@RestController
public class DrawController {
    private DrawService drawService;
    @Autowired
    public DrawController(DrawService drawService) {
        this.drawService = drawService;
    }
    @PostMapping("/draw/ecommerce")
    public void drawECommerceDesign(@RequestBody String designContent) {
        drawService.drawECommerceDesign(designContent);
    }
    @PostMapping("/draw/anime")
    public void drawAnimePoster(@RequestBody String posterContent) {
        drawService.drawAnimePoster(posterContent);
    }
    @PostMapping("/draw/business")
    public void drawBusinessPoster(@RequestBody String posterContent) {
        drawService.drawBusinessPoster(posterContent);
    }
}

DrawService

package com.spring.service;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

public interface DrawService {
    @PostMapping("/ecommerce")
    void drawECommerceDesign(@RequestBody String designContent);
    @PostMapping("/anime")
    void drawAnimePoster(@RequestBody String posterContent);
    @PostMapping("/business")
    void drawBusinessPoster(@RequestBody String posterContent);
}

DrawServiceImpl

package com.spring.service.impl;

import com.spring.service.DrawService;

public class DrawServiceImpl implements DrawService {
    @Override
    public void drawECommerceDesign(String designContent) {
        // 绘制电商设计
    }
    @Override
    public void drawAnimePoster(String posterContent) {
        // 绘制动漫海报
    }
    @Override
    public void drawBusinessPoster(String posterContent) {
        // 绘制商业海报
    }
}

更新时间 2024-02-16