ScenarioUtils,SpringBoot Bean出力フィルタツール
2532 ワード
全世界の男性の交友の大規模なコミュニティのリンクの住所:https://github.com/linwaiwai/ScenarioUtils
ScenarioUtils
ScenarioUtils
ScenarioUtilsはspring boot beanオブジェクトの出力フィルタリングのために作成された簡単なツールセットです.このツールは、あなたが作成したサービスが、異なるニーズに基づいて新しいBeanを作成する必要がなくなります.ゴムコードを書くのを少なくしてこそ、心身ともに健康になります.
使用方法:public class Project extends ScenarioModel implements Serializable {
private Integer id;
private String name;
static public final String LIST_SCENARIO = "LIST_SCENARIO";
static public final String DETAIL_SCENARIO = "DETAIL_SCENARIO";
@Override
public HashMap> scenarios(){
HashMap> scenarios = super.scenarios();
List fields = new ArrayList();
String[] attrs = {"id"};
Collections.addAll(fields, attrs);
scenarios.put(Project.LIST_SCENARIO, fields);
fields = new ArrayList();
String[] attrs1 = {"id", "name"};
Collections.addAll(fields, attrs1);
scenarios.put(Project.DETAIL_SCENARIO, fields);
return scenarios;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
}
シーンを使用:@RestController
class ProjectController {
@RequestMapping(value = "/list", method = RequestMethod.GET)
public @ResponseBody RestResponse list(){
Project project = new Project();
project.id = 1;
project.name = "hello";
project.setScenario(Project.LIST_SCENARIO);
return new RestResponse(project, 1, "ok");
}
@RequestMapping(value = "/detail", method = RequestMethod.GET)
public @ResponseBody RestResponse detail(){
Project project = new Project();
project.id = 1;
project.name = "hello";
project.setScenario(Project.DETAIL_SCENARIO);
return new RestResponse(project, 1, "ok");
}
}
出力結果:
/list出力:{code:1,message:“ok”,data:{id:1}}
/detail出力:{code:1,message:“ok”,data:{id:1,name:“hello”}
サポートを賞賛するには、文章の下にある賞賛ボタンをクリックしてください.
public class Project extends ScenarioModel implements Serializable {
private Integer id;
private String name;
static public final String LIST_SCENARIO = "LIST_SCENARIO";
static public final String DETAIL_SCENARIO = "DETAIL_SCENARIO";
@Override
public HashMap> scenarios(){
HashMap> scenarios = super.scenarios();
List fields = new ArrayList();
String[] attrs = {"id"};
Collections.addAll(fields, attrs);
scenarios.put(Project.LIST_SCENARIO, fields);
fields = new ArrayList();
String[] attrs1 = {"id", "name"};
Collections.addAll(fields, attrs1);
scenarios.put(Project.DETAIL_SCENARIO, fields);
return scenarios;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
}
@RestController
class ProjectController {
@RequestMapping(value = "/list", method = RequestMethod.GET)
public @ResponseBody RestResponse list(){
Project project = new Project();
project.id = 1;
project.name = "hello";
project.setScenario(Project.LIST_SCENARIO);
return new RestResponse(project, 1, "ok");
}
@RequestMapping(value = "/detail", method = RequestMethod.GET)
public @ResponseBody RestResponse detail(){
Project project = new Project();
project.id = 1;
project.name = "hello";
project.setScenario(Project.DETAIL_SCENARIO);
return new RestResponse(project, 1, "ok");
}
}