All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.zlyx.easyapi.web.ServiceApiController Maven / Gradle / Ivy

package com.zlyx.easyapi.web;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.zlyx.easyapi.ApiManager;
import com.zlyx.easyapi.config.ApiConfigurer;
import com.zlyx.easyapi.json.Json;
import com.zlyx.easyapi.reader.ApiReader;

import io.swagger.annotations.Api;
import io.swagger.config.SwaggerConfig;
import io.swagger.models.Swagger;

@Api(hidden = true)
@Controller
@RequestMapping("service-api")
public class ServiceApiController {

	public static final String DEFAULT_URL = "/api-docs";
	private static final String HAL_MEDIA_TYPE = "application/hal+json";

	private ApiConfigurer configurer;

	@Autowired
	public ServiceApiController(ApiConfigurer configurer) {
		this.configurer = configurer;
	}

	@RequestMapping(value = DEFAULT_URL, method = RequestMethod.GET, produces = { "application/json; charset=utf-8",
			HAL_MEDIA_TYPE })
	@ResponseBody
	public ResponseEntity getApiList(String group) throws JsonProcessingException {
		if (configurer.getApiProperties().isDisable()) {
			return new ResponseEntity(HttpStatus.NOT_FOUND);
		}
		Swagger swagger = ApiManager.getSwagger(group);
		if (null != swagger) {
			return new ResponseEntity(new Json(io.swagger.util.Json.mapper().writeValueAsString(swagger)),
					HttpStatus.OK);
		} else {
			swagger = new Swagger();
		}
		final SwaggerConfig configurator = configurer;
		if (configurator != null) {
			configurator.configure(swagger);
		}
		Map, Object> interfaceMapRef = ApiManager.interfaceMapRef(group);
		if (null != interfaceMapRef) {
			ApiReader.read(swagger, interfaceMapRef);
		}
		ApiManager.registerSwagger(group, swagger);
		return new ResponseEntity(new Json(io.swagger.util.Json.mapper().writeValueAsString(swagger)),
				HttpStatus.OK);
	}

	@ResponseBody
	@RequestMapping("/apis/get")
	public Map> getGroupNames() {
		return ApiManager.getGroupNames();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy