【练习】test1-10

package com.yuefangwang.run.controler;

import com.alibaba.fastjson.JSON;
import com.yuefangwang.run.DataMapper.entity.Ping;
import com.yuefangwang.run.service.storage.StorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.joda.time.LocalTime;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.concurrent.atomic.AtomicLong;


@RestController
@RequestMapping("/spring")
@Slf4j
public class Test1_10 {
//   1
    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();
//    2
//    private static final Logger log = LoggerFactory.getLogger(Test1_10.class);

//    4
    private final StorageService storageService;

    @Autowired
    public Test1_10(StorageService storageService) {
        this.storageService = storageService;
    }


    @GetMapping("/1")
    public Ping getAttachList1(@RequestParam(value = "name", defaultValue = "World") String name) {
//      1  Building a RestFul Web Serviec
//        目的:创建一个服务 http://localhost:8080/greeting. 返回一个Json
//          知识点:GetMapping
//        The @GetMapping annotation ensures that HTTP GET requests to /greeting are mapped to the greeting() method.
//
//                There are companion annotations for other HTTP verbs (e.g. @PostMapping for POST). There is also a @RequestMapping annotation that they all derive from, and can serve as a synonym (e.g. @RequestMapping(method=GET)).
//        @RequestParam binds the value of the query string parameter name into the name parameter of the greeting() method. If the name parameter is absent in the request, the defaultValue of World is used.
//         知识点:template
//                The implementation of the method body creates and returns a new Greeting object with id and content attributes based on the next value from the counter and formats the given name by using the greeting template.
        Ping ping = new Ping();
        ping.setThename( String.format(template, name));
        ping.setId(counter.incrementAndGet());
        return ping  ;
//        return JSON.toJSONString(ping)  ;

    }

    @GetMapping("/2")
    public String getAttachList2(HttpServletRequest request, HttpServletResponse response) {
//      2 Building a RestFul Web Serviec 复制10个修改  地址, 标题 Building a RestFul Web Serviec
//      目的:使用RestTemplate接收数据
//      You will build an application that uses Spring’s RestTemplate to retrieve a random Spring Boot quotation at https://quoters.apps.pcfone.io/api/random.
//        知识点:@JsonIgnoreProperties(ignoreUnknown = true) 可以跳过不识别的字段
//        A logger, to send output to the log (the console, in this example).
//          知识点:CommandLineRunner
//        知识点:RestTemplate
//        A RestTemplate, which uses the Jackson JSON processing library to process the incoming data.
//
//                A CommandLineRunner that runs the RestTemplate (and, consequently, fetches our quotation) on startup.
        log.info("s");
//        return args -> {
            RestTemplate restTemplate = new RestTemplate();
            Ping ping1 = restTemplate.getForObject(
                    "http://127.0.0.1:8080/spring/1?name=李四", Ping.class);
            log.info(JSON.toJSONString(ping1));
            return JSON.toJSONString(ping1);
//
//        };
    }

    @GetMapping("/3")
    public String getAttachList3(HttpServletRequest request, HttpServletResponse response) {
//      3  Building Java Projects with Maven
//        目的学习java打包
        LocalTime currentTime = new LocalTime();
        System.out.println("The current local time is: " + currentTime);
//        Greeter greeter = new Greeter();
//        System.out.println(greeter.sayHello());
        return "abc";
    }

//    @GetMapping("/4")
    @PostMapping("/4")
    public String getAttachList4(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes)   {
//      4  Uploading Files  上传文件是需要post.
//        目的:You will create a Spring Boot web application that accepts file uploads. You will also build a simple HTML interface to upload a test file.
//        In this case:
//          service.storage 是一个写好的上传包。只需要修改 StorageProperties 里的上传路径。
//        GET /: Looks up the current list of uploaded files from the StorageService and loads it into a Thymeleaf template. It calculates a link to the actual resource by using MvcUriComponentsBuilder.
//
//      GET /files/{filename}: Loads the resource (if it exists) and sends it to the browser to download by using a Content-Disposition response header.
//
//        POST /: Handles a multi-part message file and gives it to the StorageService for saving.
//     为了测试限制   Pick a (small) file and press Upload. You should see the success page from the controller. If you choose a file that is too large, you will get an ugly error page.
//        测试实例  FileUploadTests  学习写测试实例
        storageService.store(file);
        long size = file.getSize();

        log.info("本次上传的文件大小是:" + size);
//        131072  128KB =   the request was rejected because its size (399156) exceeds the configured maximum (131072)
        return "上传成功";
    }

    @GetMapping("/5")
    public String getAttachList5(HttpServletRequest request, HttpServletResponse response) {
//      5  Accessing Data with Neo4j

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值