• Spring Boot 例一 实现jsonp接口


    1.新建项目(选择quikstart)

    2.增加spring boot 依赖

        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-autoconfigure</artifactId>
          <version>1.5.2.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
          <version>1.5.2.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});

  • 相关阅读:
    恢复更新
    Sun与Google结为战略合作伙伴, 可能联合Sun推出Web Office
    Outlook数据文件备份和按年存档的办法
    新装的几个软件试用体会(WPS 2005,Opera,QQ 2005 Beta3)
    关于VBA和Excel,GIS软件
    人格测试
    Taking a serious look at grids
    iPod、iMac 5、iTunes 6
    如此“自助”
    UCGIS优先研究领域介绍
  • 原文地址:https://www.cnblogs.com/zhshlimi/p/9111283.html
Copyright © 2020-2023  润新知