zoukankan      html  css  js  c++  java
  • 吴裕雄天生自然SPRINGBOOT开发实战SpringBoot REST示例

    REST应用程序遵循REST体系结构方法。我们使用REST应用程序来开发和设计网络应用程序。它生成对数据执行CRUD操作的HTTP请求。通常,它以JSON或 XML 格式返回数据。
    <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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.ssfs</groupId>
        <artifactId>9-6-page</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.0.RELEASE</version>
            <relativePath /> <!-- lookup parent from repository -->
        </parent>
    
        <dependencies>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <!-- JSTL for JSP spring boot搭建web项目,跳转到jsp一定要添加下面的jar包,否则出现跳转的时候,变成下载的功能,保存该页面 -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
            </dependency>
    
            <!-- Need this to compile JSP -->
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-jasper</artifactId>
                <scope>provided</scope>
            </dependency>
    
            <!-- DevTools in Spring Boot 项目热部署 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
            </dependency>
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    server.port=8089
    
    spring.mvc.view.prefix=/WEB-INF/jsp/
    spring.mvc.view.suffix=.jsp
    <!DOCTYPE html>
    <html>
    <head>
    <title>Home page</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <p>
            <a href="product">Get all Products</a>
        </p>
    </body>
    </html>
    package temp;
    
    public class Product {
        private int id;
        private String pname;
        private String batchno;
        private double price;
        private int noofproduct;
    
        // default constructor
        public Product() {
    
        }
    
        // constructor using fields
        public Product(int id, String pname, String batchno, double price, int noofproduct) {
            super();
            this.id = id;
            this.pname = pname;
            this.batchno = batchno;
            this.price = price;
            this.noofproduct = noofproduct;
        }
    
        // getters and setters
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getPname() {
            return pname;
        }
    
        public void setPname(String pname) {
            this.pname = pname;
        }
    
        public String getBatchno() {
            return batchno;
        }
    
        public void setBatchno(String batchno) {
            this.batchno = batchno;
        }
    
        public double getPrice() {
            return price;
        }
    
        public void setPrice(double price) {
            this.price = price;
        }
    
        public int getNoofproduct() {
            return noofproduct;
        }
    
        public void setNoofproduct(int noofproduct) {
            this.noofproduct = noofproduct;
        }
    
    }
    package temp;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.servlet.ModelAndView;
    
    import temp.Product;
    import temp.ProductService;
    
    @Controller
    public class MyController {
        @Autowired
        private ProductService productService;
        
        @RequestMapping(value = "/index")
        public String homeIndex() {
            System.out.println("**************");
            System.out.println("redirect to home page!");
            return "homeee";
        }
        
        @GetMapping(value = "/product")
        @ResponseBody
        public List<Product> getProduct() {
            // finds all the products
            System.out.println("redirect to home page!");
            List<Product> products = productService.findAll();
            // returns the product list
            return products;
        }
        
        @RequestMapping(value = "/home")
        public String home() {
            System.out.println("redirect to home page!");
            return "index";
        }
    
        @RequestMapping(value = "/home/page")
        @ResponseBody
        public ModelAndView goHome() {
            System.out.println("go to the home page!");
            ModelAndView mode = new ModelAndView();
            mode.addObject("name", "天生自然");
            mode.setViewName("index");
            return mode;
        }
        
        
    }
    package temp;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.springframework.stereotype.Service;
    
    import temp.IProductService;
    import temp.Product;
    
    @Service
    public class ProductService implements IProductService {
    
        @Override
        public List<Product> findAll() {
            // TODO Auto-generated method stub
            // creating an object of ArrayList
            ArrayList<Product> products = new ArrayList<Product>();
            // adding products to the List
            products.add(new Product(100, "Mobile", "CLK98123", 9000.00, 6));
            products.add(new Product(101, "Smart TV", "LGST09167", 60000.00, 3));
            products.add(new Product(102, "Washing Machine", "38753BK9", 9000.00, 7));
            products.add(new Product(103, "Laptop", "LHP29OCP", 24000.00, 1));
            products.add(new Product(104, "Air Conditioner", "ACLG66721", 30000.00, 5));
            products.add(new Product(105, "Refrigerator ", "12WP9087", 10000.00, 4));
            // returns a list of product
            return products;
        }
    
    }
    package temp;
    
    import java.util.List;
    
    import temp.Product;
    
    public interface IProductService {
        List<Product> findAll();
    }
    package temp;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class MySpringApplication {
        public static void main(String[] args) {
            SpringApplication.run(MySpringApplication.class);
        }
    }

     

     

     

  • 相关阅读:
    【转】Odoo开发之:工作流 workflow
    【转】Odoo:基本字段类型
    【转】odoo 10的企业微信发送程序介绍
    Installing python-ldap in Ubuntu
    Odoo8中安装新模块找不到的问题
    mybatis缓存
    ThreadPoolExecutor线程池进阶使用
    使用Dubbo实现RPC调用
    java静态代理模式
    java四种线程池
  • 原文地址:https://www.cnblogs.com/tszr/p/15239199.html
Copyright © 2011-2022 走看看