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接口的更多相关文章

  1. Spring Boot启动过程及回调接口汇总

    Spring Boot启动过程及回调接口汇总 链接: https://www.itcodemonkey.com/article/1431.html 来自:chanjarster (Daniel Qia ...

  2. Spring Boot的数据访问:CrudRepository接口的使用

    示例 使用CrudRepository接口访问数据 创建一个新的Maven项目,命名为crudrepositorytest.按照Maven项目的规范,在src/main/下新建一个名为resource ...

  3. spring boot使用swagger生成api接口文档

    前言 在之前的文章中,使用mybatis-plus生成了对应的包,在此基础上,我们针对项目的api接口,添加swagger配置和注解,生成swagger接口文档 具体可以查看本站spring boot ...

  4. 精尽Spring Boot源码分析 - Condition 接口的扩展

    该系列文章是笔者在学习 Spring Boot 过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring Boot 源码分析 GitHub 地址 进行阅读 Sprin ...

  5. Spring Boot 集成 Swagger,生成接口文档就这么简单!

    之前的文章介绍了<推荐一款接口 API 设计神器!>,今天栈长给大家介绍下如何与优秀的 Spring Boot 框架进行集成,简直不能太简单. 你所需具备的基础 告诉你,Spring Bo ...

  6. Spring Boot AOP 扫盲,实现接口访问的统一日志记录

    AOP 是 Spring 体系中非常重要的两个概念之一(另外一个是 IoC),今天这篇文章就来带大家通过实战的方式,在编程猫 SpringBoot 项目中使用 AOP 技术为 controller 层 ...

  7. Spring boot 添加日志 和 生成接口文档

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...

  8. Spring Boot使用AOP实现REST接口简易灵活的安全认证

    我们继续上一篇文章的分析,本文将通过AOP的方式实现一个相对更加简易灵活的API安全认证服务. 我们先看实现,然后介绍和分析AOP基本原理和常用术语. 一.Authorized实现 1.定义注解 pa ...

  9. spring boot 通过feign调用api接口

    目的:远程调用服务器api,直接上步骤: 1,添加maven依赖,这是必须的: <dependency> <groupId>org.springframework.cloud& ...

随机推荐

  1. ssh试卷

    2.简述Hibernate的工作原理. 答:首先,Configuration读取Hibernate的配置文件及映射文件中的信息,即加载配置文件和映射文件,并通过Hibernate配置文件生成一个多线程 ...

  2. HDU 3333 Turing Tree (主席树)

    题意:给定上一个序列,然后有一些询问,求区间 l - r 中有多少个不同的数的和. 析:和求区间不同数的方法是一样,只要用主席树维护就好. 代码如下: #pragma comment(linker, ...

  3. JQuery解决事件动画重复问题

    开发项目时,经常要写动画效果,有时候会遇到动画重复问题,例如:当鼠标移动到某个元素上时,执行某个动画,当我鼠标多次移动到该元素时,该动画就要连续执行,那么怎么去解决呢? 话不多说,直接添代码,简单明了 ...

  4. 编写高质量代码改善C#程序的157个建议——建议15: 使用dynamic来简化反射实现

    建议15: 使用dynamic来简化反射实现 dynamic是Framework 4.0的新特性.dynamic的出现让C#具有了弱语言类型的特性.编译器在编译的时候不再对类型进行检查,编译器默认dy ...

  5. 20169219 SEED SQL注入实验

    实验环境SEED Ubuntu镜像 环境配置 实验需要三样东西,Firefox.apache.phpBB2(镜像中已有): 1.运行Apache Server:只需运行命令sudo service a ...

  6. adb命令安装及卸载应用

    一.手机连接电脑,检测手机是否已开启授权并连接成功 adb devices 二.安装应用 adb install UYUN-CARRIER-Android.apk 三.卸载应用 1.查看应用包名 ad ...

  7. nagios+influxdb+grafana的监控数据可视化流程

    nagios介绍 nagios是一款开源监控的应用,可用于监控本地和远程主机的日志.资源.死活等等诸多功能.通过snmp协议和nrpe协议. nagios的配置文件是由nconf上进行配置,然后点击生 ...

  8. docker--基本命令

    仅做学习参考,可能有误 part1:启动docker服务 在Windows上使用MySQL时候,有时无法直接使用MySQL -uroot -p 来进入MySQL,这是因为我们没有启动会MySQL服务此 ...

  9. C# winform调用类似按钮点击的事件时自带参数该怎么写

    //按钮事件 private void btn_Click(object sender, EventArgs e) {} //自己的函数 private void myFunc() { //程序中其他 ...

  10. 【python】collections模块(有序字典,计数器,双向队列)

    collections模块基本介绍 我们都知道,Python拥有一些内置的数据类型,比如str, int, list, tuple, dict等, collections模块在这些内置数据类型的基础上 ...