Springboot根據url后綴返回json或者xml或者html
Springboot根據url后綴.
返回json或者xml;
根據后綴名稱返回html。
作者:liuren版權聲明:本文為原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
初始化Springboot項目
一、首先引入的pom.xml文件包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.codepeople</groupId>
<artifactId>springboot-json-xml</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-json-xml</name>
<description>Springboot根據url后綴返回json或者xml或者html</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
項目結構圖
開始項目配置
application.properties
spring.thymeleaf.mode=HTML #后綴名稱
spring.thymeleaf.encoding=utf-8 #編碼格式
spring.thymeleaf.cache=false #是否使用緩存
Springboot配置
WebMvcConfig.java
package cn.codepeople.web.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
@Override
protected void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(true) //是否支持后綴的方式
.parameterName("mediaType")
.defaultContentType(MediaType.APPLICATION_JSON)
.mediaType("xml", MediaType.APPLICATION_XML) //當后綴名稱為xml的時候返回xml數據
.mediaType("html", MediaType.TEXT_HTML) //當后綴名稱是html時候返回html頁面
.mediaType("json", MediaType.APPLICATION_JSON);//當后綴名稱是json的時候返回json數據
}
}
新建Bean實體類類
User.java
package cn.codepeople.web.entity;
import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class User {
private Long id;
private String userName;
private String age;
private Date birthdy;
private boolean delFlag;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public Date getBirthdy() {
return birthdy;
}
public void setBirthdy(Date birthdy) {
this.birthdy = birthdy;
}
public boolean isDelFlag() {
return delFlag;
}
public void setDelFlag(boolean delFlag) {
this.delFlag = delFlag;
}
}
新建UserService.java接口
UserService.java
package cn.codepeople.web.service;
import java.util.List;
import cn.codepeople.web.entity.User;
public interface UserService {
List<User> listUser();
}
新建UserServiceImpl實現類
UserServiceImpl.java
package cn.codepeople.web.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Service;
import cn.codepeople.web.entity.User;
import cn.codepeople.web.service.UserService;
@Service
public class UserServiceImpl implements UserService {
@Override
public List<User> listUser() {
List<User> list = new ArrayList<User>();
User user = new User();
user.setId(1L);
user.setAge("12");
user.setDelFlag(true);
user.setUserName("zhang1");
user.setBirthdy(new Date());
list.add(user);
User userx = new User();
userx.setId(2L);
userx.setAge("23");
userx.setDelFlag(true);
userx.setUserName("zhang2");
userx.setBirthdy(new Date());
list.add(userx);
return list;
}
}
新建controller類
package cn.codepeople.web.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.codepeople.web.entity.User;
import cn.codepeople.web.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/list")
@ResponseBody
private List<User> listUser(){
return userService.listUser();
}
@RequestMapping("/index")
public String user() {
return "list.html";
}
}
修改SpringbootJsonXmlApplication.java啟動類
package cn.codepeople.web;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootJsonXmlApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootJsonXmlApplication.class, args);
}
}
templates目錄下新建list.html文件
list.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
Springboot根據url后綴返回json或者xml或者html ->>>
</body>
</html>
啟動成功頁面
訪問地址是:http://127.0.0.1:8080/user/list.json
訪問地址是:<http://127.0.0.1:8080/user/list.xml
訪問地址是:<http://127.0.0.1:8080/user/index.html
有一點我沒明白,如果大家有深刻的理解歡迎幫我解答一下。WebMvcConfig.java中默認格式是json
但如果不加后綴返回的是xml格式文件,是因為我沒在controller中做默認格式配置嗎?如果不加后綴按照
WebMvcConfig.java配置不是應該返回json嘛?
博客地址:https://www.codepeople.cn
=====================================================================
微信公眾號: