The data returned in the controller depends on a core annotation @ReponseBody
@RestController is composed of two annotations, including @Controller and @ResponseBody
The above return form is too random and inconvenient to maintain, so now we need a unified response result
Add a new Result class under pojo
package com.example.pojo; /** * Unified response result encapsulation class */ public class Result { private Integer code ;//1 success, 0 failure private String msg; //Prompt message private Object data; //data data public Result() { } public Result(Integer code, String msg, Object data) { this.code = code; this.msg = msg; this.data = data; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } public static Result success(Object data){ return new Result(1, "success", data); } public static Result success(){ return new Result(1, "success", null); } public static Result error(String msg){ return new Result(0, msg, null); } @Override public String toString() { return "Result{" + "code=" + code + ", msg='" + msg + ''' + ", data=" + data + '}'; } }
package com.example.controller; import com.example.pojo.Address; import com.example.pojo.Result; import com.example.pojo.Address; import com.example.pojo.Result; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.List; /** * Test response data */ @RestController public class ResponseController { /*@RequestMapping("/hello") public String hello(){ System.out.println("Hello World ~"); return "Hello World ~"; } @RequestMapping("/getAddr") public Address getAddr(){ Address addr = new Address(); addr.setProvince("Guangdong"); addr.setCity("Shenzhen"); return addr; } @RequestMapping("/listAddr") public List<Address> listAddr(){ List<Address> list = new ArrayList<>(); Address addr = new Address(); addr.setProvince("Guangdong"); addr.setCity("Shenzhen"); Address addr2 = new Address(); addr2.setProvince("Shaanxi"); addr2.setCity("Xi'an"); list. add(addr); list. add(addr2); return list; }*/ @RequestMapping("/hello") public Result hello(){ System.out.println("Hello World ~"); //return new Result(1,"success","Hello World ~"); return Result. success("Hello World ~"); } @RequestMapping("/getAddr") public Result getAddr(){ Address addr = new Address(); addr.setProvince("Guangdong"); addr.setCity("Shenzhen"); return Result. success(addr); } @RequestMapping("/listAddr") public Result listAddr(){ List<Address> list = new ArrayList<>(); Address addr = new Address(); addr.setProvince("Guangdong"); addr.setCity("Shenzhen"); Address addr2 = new Address(); addr2.setProvince("Shaanxi"); addr2.setCity("Xi'an"); list. add(addr); list. add(addr2); return Result. success(list); } }
Three different forms of access response data
Case:
Case flow?
Step 1: Import dependencies
<dependency> <groupId>org.dom4j</groupId> <artifactId>dom4j</artifactId> <version>2.1.3</version> </dependency>
Step 2: Introduce tool classes
package com.example.utils; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; import java.io.File; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; public class XmlParserUtils { public static <T> List<T> parse(String file, Class<T> targetClass) { ArrayList<T> list = new ArrayList<T>(); //Encapsulate the parsed data try { //1. Get a parser object SAXReader saxReader = new SAXReader(); //2. Use the parser to load the xml file into memory and return a document object Document document = saxReader. read(new File(file)); //3. Get the root tag Element rootElement = document. getRootElement(); //4. Get the user tag through the root tag List<Element> elements = rootElement. elements("emp"); //5. Traverse the collection to get each user tag for (Element element : elements) { //Get the name attribute String name = element. element("name"). getText(); //Get the age property String age = element. element("age"). getText(); //Get the image attribute String image = element. element("image"). getText(); //Get the gender attribute String gender = element. element("gender"). getText(); // Get job attributes String job = element. element("job"). getText(); //Assembly data Constructor<T> constructor = targetClass.getDeclaredConstructor(String.class, Integer.class, String.class, String.class, String.class); constructor. setAccessible(true); T object = constructor. newInstance(name, Integer. parseInt(age), image, gender, job); list. add(object); } } catch (Exception e) { e.printStackTrace(); } return list; } }
Introduce entity class to encapsulate employee data
package com.example.pojo; public class Emp { private String name; private Integer age; private String image; private String gender; private String job; public Emp() { } public Emp(String name, Integer age, String image, String gender, String job) { this.name = name; this. age = age; this.image = image; this.gender = gender; this.job = job; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this. age = age; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } @Override public String toString() { return "Emp{" + "name='" + name + ''' + ", age=" + age + ", image='" + image + ''' + ", gender='" + gender + ''' + ", job='" + job + ''' + '}'; } }
import XML file
<?xml version="1.0" encoding="UTF-8" ?> <emps> <emp> <name>Golden Retriever</name> <age>55</age> <image>https://web-framework.oss-cn-hangzhou.aliyuncs.com/web/1.jpg</image> <!-- 1: male, 2: female --> <gender>1</gender> <!-- 1: Lecturer, 2: Class teacher, 3: Employment guidance --> <job>1</job> </emp> <emp> <name>Eagle King</name> <age>65</age> <image>https://web-framework.oss-cn-hangzhou.aliyuncs.com/web/2.jpg</image> <gender>1</gender> <job>1</job> </emp> <emp> <name>Blue Wing Bat King</name> <age>45</age> <image>https://web-framework.oss-cn-hangzhou.aliyuncs.com/web/3.jpg</image> <gender>1</gender> <job>2</job> </emp> <emp> <name>Zishan Dragon King</name> <age>38</age> <image>https://web-framework.oss-cn-hangzhou.aliyuncs.com/web/4.jpg</image> <gender>2</gender> <job>3</job> </emp> </emps>
Note:
Static pages in springboot should be stored in the following three directories
Creation of the Controller class
Define a Controller class as follows and write an interface and corresponding access path
To load an XML file, you need to know the path of the XML file, as follows
String file=this.getClass().getClassLoader().getResource("emp.xml").getFile();
Then parse the XML and put its content into an entity class object
List<Emp> empList =XmlParserUtils.parse(file, Emp.class);
Finally, according to the requirements, the data inside should be in the required format.
/2. Transform data - gender, job empList.stream().forEach(emp->{ //Process gender 1: male, 2: female String gender = emp. getGender(); if("1".equals(gender)){ emp.setGender("Male"); }else if("2".equals(gender)){ emp.setGender("Female"); } //Process job - 1: Lecturer, 2: Class teacher, 3: Employment guidance String job = emp. getJob(); if("1".equals(job)){ emp.setJob("Lecturer"); }else if("2".equals(job)){ emp.setJob("Class teacher"); }else if("3".equals(job)){ emp.setJob("Employment Guidance"); } });
return response data
return Result. success(empList);
The access output is as follows, Note: Do not have Chinese or other illegal characters in the path
Front-end display
?
The front end gets the response data through the axios request in the hook function and assigns it to tableData and displays it