Spring Boot 例一 实现jsonp接口
1.新建项目(选择quikstart)
2.增加spring boot 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>1.5..RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5..RELEASE</version>
</dependency>
3.添加 springboot打包jar 插件依赖
参考 Spring Boot的Maven插件Spring Boot Maven plugin详解
<build>
<plugins>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<configuration>
<encoding>UTF-8</encoding>
<charset>UTF-8</charset>
<docencoding>UTF-8</docencoding>
<skip>true</skip>
</configuration>
</plugin> <plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
4. 设置启动
application.properties 设置
server.port=9089
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.SpringApplication;
/**
* Hello world!
*
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
5. 添加 Contrller层
import com.g2.webtest.model.Person;
import com.g2.webtest.model.PersonReq;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map; @RestController
@RequestMapping("/h5")
public class HomeController {
private static Map<String, Person> dataSource;
private static Person anonymous = new Person("未知", 0); @RequestMapping(value = "/{code}.jsonp")
public Person testJsonp(@PathVariable("code") String code) {
return dataSource.getOrDefault(code, anonymous);
} @RequestMapping(value = "/person", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType
.APPLICATION_JSON_VALUE)
@ResponseBody
public Person testJsonp2(@RequestBody PersonReq req) {
return dataSource.getOrDefault(req.getCode(), anonymous);
} static {
dataSource = new HashMap<>();
dataSource.put("1001", new Person("张三", 15));
dataSource.put("1002", new Person("李四", 18));
}
}
public class Person {
private String name;
private int age;
public Person(){
this("",0);
}
public Person(String name,int age){
this.name=name;
this.age=age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
6.设置jsonp的配置
import com.g2.webtest.controller.h5.HomeController; import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.AbstractJsonpResponseBodyAdvice; @ControllerAdvice(basePackageClasses = {HomeController.class})
public class JSONPConfiguration extends AbstractJsonpResponseBodyAdvice {
public JSONPConfiguration(){
super("callback","jsonp");
}
}
7.测试
http://127.0.0.1:9089/h5/1001.jsonp?callback=myfun
myfun({"name":"张三","age":15});
Spring Boot 例一 实现jsonp接口的更多相关文章
- Spring Boot启动过程及回调接口汇总
Spring Boot启动过程及回调接口汇总 链接: https://www.itcodemonkey.com/article/1431.html 来自:chanjarster (Daniel Qia ...
- Spring Boot的数据访问:CrudRepository接口的使用
示例 使用CrudRepository接口访问数据 创建一个新的Maven项目,命名为crudrepositorytest.按照Maven项目的规范,在src/main/下新建一个名为resource ...
- spring boot使用swagger生成api接口文档
前言 在之前的文章中,使用mybatis-plus生成了对应的包,在此基础上,我们针对项目的api接口,添加swagger配置和注解,生成swagger接口文档 具体可以查看本站spring boot ...
- 精尽Spring Boot源码分析 - Condition 接口的扩展
该系列文章是笔者在学习 Spring Boot 过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring Boot 源码分析 GitHub 地址 进行阅读 Sprin ...
- Spring Boot 集成 Swagger,生成接口文档就这么简单!
之前的文章介绍了<推荐一款接口 API 设计神器!>,今天栈长给大家介绍下如何与优秀的 Spring Boot 框架进行集成,简直不能太简单. 你所需具备的基础 告诉你,Spring Bo ...
- Spring Boot AOP 扫盲,实现接口访问的统一日志记录
AOP 是 Spring 体系中非常重要的两个概念之一(另外一个是 IoC),今天这篇文章就来带大家通过实战的方式,在编程猫 SpringBoot 项目中使用 AOP 技术为 controller 层 ...
- Spring boot 添加日志 和 生成接口文档
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...
- Spring Boot使用AOP实现REST接口简易灵活的安全认证
我们继续上一篇文章的分析,本文将通过AOP的方式实现一个相对更加简易灵活的API安全认证服务. 我们先看实现,然后介绍和分析AOP基本原理和常用术语. 一.Authorized实现 1.定义注解 pa ...
- spring boot 通过feign调用api接口
目的:远程调用服务器api,直接上步骤: 1,添加maven依赖,这是必须的: <dependency> <groupId>org.springframework.cloud& ...
随机推荐
- opennebule 创建cdrom数据发送
{","csrftoken":"b9b5026f1a92180b789971ed8e21d28b"}
- 在C语言中如何嵌入python脚本
最近在写配置文件时,需要使用python脚本,但脚本是一个监控作用,需要它一直驻留在linux中运行,想起C语言中能够使用deamon函数来保留一个程序一直运行,于是想到写一个deamon,并在其中嵌 ...
- CompositePattern(23种设计模式之一)
设计模式六大原则(1):单一职责原则 设计模式六大原则(2):里氏替换原则 设计模式六大原则(3):依赖倒置原则 设计模式六大原则(4):接口隔离原则 设计模式六大原则(5):迪米特法则 设计模式六大 ...
- Linux 编译内核
编译内核步骤: 1.先查看自己OS使用的内核版本 mrzhang@mrzhang:~$ uname -r4.4.0-51-genericmrzhang@mrzhang:~$ 2.如果安装系统时,自动安 ...
- 2、python的print函数格式化输出
1.格式化输出浮点数(float) pi = 3.141592653 print('%10.3f' % pi) #字段宽10,精度3 ,结果 print('%010.3f' % pi) #用0填充 ...
- linux下mariadb的下载与卸载
Linux下mariadb的安装 使用阿里云的mariadb yum install mariadb-server mariadb -y 启动mariadb数据库 systemctl start/st ...
- Dapper多表查询
1对1 //文章 public class Post { public int ID { get; set; } public string Title { get; set; } public st ...
- Vue 父组件向子组件传值,传方法,传父组件整体
父子组件传值 1.父组件调用子组件时绑定属性,例如-> :title="title" 2.子组件中在props中声明title:props:['title','msg'] 3 ...
- SQLServer如何在批量插入后,获取批量插入的自增列的值
解决方法如下: Use the OUTPUT functionality to grab all the INSERTED Id back into a table. 使用output 功能获取所有插 ...
- kali linux之手动漏洞挖掘三(sql注入)
服务器端程序将用户输入作为参数作为查询条件,直接拼写sql语句,并将结果返回给客户端浏览器 如判断登录 select * from users where user='uname' and passw ...