swagger( API 框架)的涉及:
在代码中使用自定义的注解来生成接口文档,这个在前后端分离的项目中很重要。这样做的好处是 在开发接口时可以通过swagger 将接口文档定义好,同时也方便以后的维护
由此我也真正接触到后端的真正工作,提供接口,也由此明白下发的word文档的意义——没有swagger之前,我们可以使用word,excel等功能来书写接口定义文档。实际项目中非常需要写文档,提高Java服务端和Web前端以及移动端的对接效率。
继上一次代码 进行的优化 比如添加方法上的注解 及其判定数据类型赋值为null 或者为0等等。
EntityProperty.java:
package com.boot.demo.service;
import java.util.List;
public class EntityProperty {
private String packageName;
private String className;
private List filedPropertyList;//字段属性集合
public EntityProperty() {
}
public EntityProperty(String packageName, String className) {
this.packageName = packageName;
this.className = className;
}
public EntityProperty(String packageName, String className, List filedPropertyList) {
this.packageName = packageName;
this.className = className;
this.filedPropertyList = filedPropertyList;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public List getFiledPropertyList() {
return filedPropertyList;
}
public void setFiledPropertyList(List filedPropertyList) {
this.filedPropertyList = filedPropertyList;
}
@Override
public String toString() {
return "EntityProperty{" +
"packageName='" + packageName + ''' +
", className='" + className + ''' +
", filedPropertyList=" + filedPropertyList +
'}';
}
}
FieldProperty.java:
package com.boot.demo.service;
public class FieldProperty {
private String name;
private String type;
private String annotationName;
public FieldProperty()
{
}
public FieldProperty(String name, String type) {
this.name = name;
this.type = type;
}
public FieldProperty(String name, String type,String annotationName) {
this.name = name;
this.type = type;
this.annotationName=annotationName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getannotationName() {
return annotationName;
}
public void setannotationName(String annotationName) {
this.annotationName = annotationName;
}
@Override
public String toString() {
return "FieldInfo{" +
"name='" + name + ''' +
", type=" + type +
", annotationName='" + annotationName + ''' +
'}';
}
}
ParseConfig.java:
package com.boot.demo.service;
public class ParseConfig {
private boolean useLombok=true;
private boolean serializable=true;
private boolean showHeader = true;
private String header = "预算调整报告数据";
//预算调整分析
public ParseConfig() {
}
public boolean isUseLombok() {
return useLombok;
}
public boolean isSerializable() {
return serializable;
}
public boolean isShowHeader() {
return showHeader;
}
public String getHeader() {
return header;
}
}
WordPOI.java:
package com.boot.demo.service;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.hwpf.usermodel.TableCell;
import org.apache.poi.hwpf.usermodel.TableIterator;
import org.apache.poi.hwpf.usermodel.TableRow;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/ * 解析配置支持自定义,具体在ParseConfig.java相关配置*/
public class WordPOI {
public WordPOI(){
}
public void generateEntity(EntityProperty entityProperty, boolean isDoc, String outPath,ParseConfig parseConfig) throws IOException{
String className = entityProperty.getClassName();
String javaName = className + ".java";
StringBuffer buffer = new StringBuffer();
buffer.append("package "+ entityProperty.getPackageName()+";nn");
//是否使用Lobmok
boolean useLombok = parseConfig.isUseLombok();
//是否导入注解
boolean importAnnotation= true;
//是否导入BigDecimal
boolean importBigDecimal = true;
List filedPropertyList = entityProperty.getFiledPropertyList();
if(filedPropertyList !=null ){
StringBuffer bufferField = new StringBuffer();
int index = 0;
int size = filedPropertyList.size();
for(FieldProperty filedProperty : filedPropertyList){//遍历数据类型
bufferField.append("t@" + filedProperty.getannotationName() +"(value = " ")"+ "n");
if (filedProperty.getName().equals("year")||filedProperty.getName().equals("time"))
bufferField.append("tprivate " + filedProperty.getType() + " " + filedProperty.getName() +"= LocalDate.now().getYear()"+";nn");
else if (filedProperty.getType().equals("BigDecimal")&&filedProperty.getName().contains("Ratio")) {
filedProperty.setType("double");
bufferField.append("tprivate " + filedProperty.getType() + " " + filedProperty.getName() + "= 0" + ";nn");
}
else if(filedProperty.getType().equals("BigDecimal"))
bufferField.append("tprivate " + filedProperty.getType() + " " + filedProperty.getName() +"= new BigDecimal("0")"+";nn");
else
bufferField.append("tprivate " + filedProperty.getType() + " " + filedProperty.getName() +"= null"+";nn");
String fieldType = filedProperty.getType();
if(fieldType!=null)
{
//遍历
if(fieldType.startsWith("BigDecimal")){
importBigDecimal = true;
}
}
index++;
}
if(parseConfig.isSerializable()){//导入Serializable
buffer.append("import java.io.Serializable;n");
}
if(importAnnotation){
buffer.append("import io.swagger.annotations.ApiModelProperty;n");
buffer.append("import lombok.experimental.Accessors;n");
buffer.append("import java.time.LocalDate;n");
}
if(importBigDecimal){
buffer.append("import java.math.BigDecimal;n");
}
if(useLombok){//导入Lombok
buffer.append("import lombok.Data;n");
}
buffer.append("n");
//是否显示头部注释说明
if(parseConfig.isShowHeader()){
String header = parseConfig.getHeader();
if(header!=null && header.length()>0){
buffer.append("n");
}
}
if(useLombok){//添加Lombok的data注解
buffer.append("@Datan");
buffer.append("@Accessors(chain = true)n");
}
//拼接类
buffer.append("public class " + className);
if(genericity){
buffer.append("");
}
//序列化
if(parseConfig.isSerializable()){
buffer.append(" implements Serializable");
}
buffer.append(" {nn");
//添加字段
buffer.append(bufferField).append("n");
}
}
buffer.append("n}nn");
//做输出工作
File dirFile = new File(outPath);
if(dirFile!=null && !dirFile.exists()){
dirFile.mkdirs();
}
//将内容写入到文件
File file = new File(outPath,javaName);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),parseConfig.getCharsetName()));
//doc格式的文档解析后会有u0007字符需统一过滤
writer.write(isDoc ? buffer.toString().replace("u0007","") : buffer.toString());
writer.flush();
writer.close();
}
}
WordPoiImpl.java:
package com.boot.demo.output;
import com.boot.demo.service.EntityProperty;
import com.boot.demo.service.FieldProperty;
import com.boot.demo.service.ParseConfig;
import com.boot.demo.service.WordPOI;
import org.apache.poi.ooxml.POIXMLDocument;
import org.apache.poi.ooxml.extractor.POIXMLTextExtractor;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class WordPoiImpl {
public static void main(String[] args) throws IOException {
WordPOI wordPOI=new WordPOI();
String BigDecimal="BigDecimal";
List list = new ArrayList<>();
List fieldProperties=new ArrayList<>();
ParseConfig parseConfig=new ParseConfig();
try {
// OPCPackage opcPackage = POIXMLDocument.openPackage("C:\Users\Redic\IdeaProjects\demo\src\main\resources\word\ystz.docx");
OPCPackage opcPackage = POIXMLDocument.openPackage("C:\Users\Redic\IdeaProjects\demo\src\main\resources\word\ystz_dept.docx");
POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
String text2007 = extractor.getText();
String str1=text2007.replaceAll("\{\{","}}");
String subSentences[] = str1.split("\}\}");
String allStr="";
for (String sub : subSentences) {
sub=sub.replaceAll("[^a-z^A-Z]", "");
allStr+=sub+" ";
}
String subNew[]=allStr.split(" ");
for (String no:subNew) {
if(!no.isEmpty())
list.add(no);
}
for (int i = 0; i < list.size(); i++) {
for (int j = i+1; j
if (list.get(i).equals(list.get(j)))
list.remove(j);
}
}
// Iterator it = list.iterator();测试是否 去除
// while (it.hasNext()) {
// System.out.println(it.next());
// }
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < list.size(); i++)
{
if(list.get(i).equals("year")||list.get(i).equals("time")) {
FieldProperty fieldProperty1 = new FieldProperty(list.get(i), "int", "ApiModelProperty");
fieldProperties.add(fieldProperty1);
}
else if(list.get(i).equals("quarter")||list.get(i).equals("dept"))
fieldProperties.add(new FieldProperty(list.get(1),"String","ApiModelProperty"));
else
fieldProperties.add(new FieldProperty(list.get(i),BigDecimal,"ApiModelProperty"));
}
// EntityProperty entityProperty=new EntityProperty("com.boot.demo.pojo","BudgetAdjustDeptReportModel",fieldProperties);
EntityProperty entityProperty=new EntityProperty("com.boot.demo.pojo","BudgetAdjustDeptAnalyseModel",fieldProperties);
wordPOI.generateEntity(entityProperty,false,"C:\Users\Redic\IdeaProjects\demo\src\main\java\com\boot\demo\pojo",parseConfig);
}
}



