ywlcn / SpringStudy

TempSampleSource
0 stars 0 forks source link

Memo #8

Open ywlcn opened 4 years ago

ywlcn commented 4 years ago

https://github.com/gapperdan/hello-springmvc https://www.concretepage.com/

Order and Export Modify

@RequestParamアノテーションを指定すると、URLに含まれるクエリパラメータや、メッセージボディーに含まれるポストパラメータを受け取ることができます。

@RequestBodyリクエストのボディを受け取ることができます。

@RequestHeader リクエストヘッダを受け取ります。

@PathVariable /api/get/1のように、REST形式のURIからパラメータを取るのに利用します。

@CookieValue クッキーの値を受け取ります。

@RequestAttribute はHttpServletRequestオブジェクトから取得される

ModelAttribute SessionAttribute RequestAttribute

ywlcn commented 4 years ago

apply plugin: 'idea' apply plugin: 'java' apply plugin: 'tomcat' apply plugin: 'war'

tomcatRun { httpPort = 8080 contextPath = "/hello-springmvc" }

repositories { mavenCentral()

}

dependencies { providedCompile 'javax.servlet:servlet-api:2.5'

def springVersion = '5.1.0.RELEASE'
compile "org.springframework:spring-core:${springVersion}"
compile "org.springframework:spring-web:${springVersion}"
compile "org.springframework:spring-webmvc:${springVersion}"

def tomcatVersion = '9.0.1'
tomcat "org.apache.tomcat:catalina:${tomcatVersion}",
        "org.apache.tomcat:coyote:${tomcatVersion}",
        "org.apache.tomcat:jasper:${tomcatVersion}"

// def tomcatVersion = '9.0.1' // tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}", // "org.apache.tomcat.embed:tomcat-embed-logging-juli:9.0.0.M6", // "org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}" //

}

buildscript { repositories { jcenter() }

dependencies {
    classpath 'org.gradle.api.plugins:gradle-tomcat-plugin:1.0'
}

}

ywlcn commented 4 years ago

package com.restapitest.control;

import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Optional;

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

import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.CookieValue; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile;

import com.restapitest.control.entity.BookInfo;

@RestController @RequestMapping(value = "/API") public class ParamTest {

// 1 http://localhost:8080/API/searchList
@RequestMapping(value = "/searchList", method = RequestMethod.GET)
@ResponseBody
public List<BookInfo> searchList() {
    ArrayList<BookInfo> list = new ArrayList<BookInfo>();
    BookInfo temp = new BookInfo();
    temp.setBookName("Default");
    list.add(temp);
    return list;
}

// 2 http://localhost:8080/API/searchListByName?name=myname
@RequestMapping(value = "/listByName", method = RequestMethod.POST)
@ResponseBody
public List<BookInfo> searchList(@RequestParam(value = "name", required = true) String name) {
    ArrayList<BookInfo> list = new ArrayList<BookInfo>();
    BookInfo temp = new BookInfo();
    temp.setBookName(name);
    list.add(temp);
    return list;
}

// 
@RequestMapping(value = "/listByNameJSON", method = RequestMethod.POST)
@ResponseBody
public List<BookInfo> searchList(@RequestBody BookInfo user) {
    ArrayList<BookInfo> list = new ArrayList<BookInfo>();
    BookInfo temp = new BookInfo();
    list.add(user);
    return list;
}

//3 * JSON Body: {"bookName":"Yuest","writer":"Jhon"}
//  * Header:Content-Type : application/json
//  * URL:http://localhost:8080/API/addUser
@RequestMapping(value = "/addUser", method = RequestMethod.POST)
@ResponseBody
public BookInfo addUser(@RequestBody BookInfo user) {
    ArrayList<BookInfo> list = new ArrayList<BookInfo>();
    list.add(user);
    return user;
}   

// 4  URL header
// URL:http://localhost:8080/API/searchListByName/name
@RequestMapping(value="/searchListByName/{name}",method = RequestMethod.POST)
@ResponseBody
public List<BookInfo> searchListByName(@PathVariable("name") String name) {
    ArrayList<BookInfo> list = new ArrayList<BookInfo>();
    BookInfo temp = new BookInfo();
    temp.setBookName(name);
    list.add(temp);
    return list;
}   

// 5  URL header
// URL:http://localhost:8080/API/searchListByDate/2-10
@RequestMapping(value="/searchListByDate/{month}-{day}",method = RequestMethod.POST)
@ResponseBody
public List<BookInfo> searchList(@PathVariable("month") int name,
                                    @PathVariable("day") int writer) {
    ArrayList<BookInfo> list = new ArrayList<BookInfo>();
    BookInfo temp = new BookInfo();
    temp.setBookName(String.valueOf(name));
    temp.setWriter(String.valueOf(writer));
    list.add(temp);
    return list;
}   

//  6
// Header:Content-Type : application/x-www-form-urlencoded
// URL:http://localhost:8080/demo/findUsersByName?isEnable=1&name=cool
//
@RequestMapping(value="/findUsersByName",method = RequestMethod.POST)
@ResponseBody
public List<BookInfo> findUsersByName(boolean isEnable , String name)
{
    ArrayList<BookInfo> list = new ArrayList<BookInfo>();
    BookInfo temp = new BookInfo();
    temp.setBookName(String.valueOf(name));
    temp.setWriter(String.valueOf(isEnable));
    list.add(temp);
    return list;
}

@RequestMapping("/hello")
@ResponseBody
public List<BookInfo> hello(@CookieValue(name = "name", defaultValue = "") String name) {
    ArrayList<BookInfo> list = new ArrayList<BookInfo>();
    BookInfo temp = new BookInfo();
    temp.setBookName("@CookieValue" + name);
    list.add(temp);
    return list;
}

@RequestMapping("/hello")
@ResponseBody
public List<BookInfo> hello(@RequestHeader(name = "User-Agent", required = false) Optional<String> name) {
    ArrayList<BookInfo> list = new ArrayList<BookInfo>();
    BookInfo temp = new BookInfo();
    temp.setBookName("hello");
    list.add(temp);
    return list;
}

// 7
@RequestMapping("upload")
@ResponseBody
public List<BookInfo> upload(HttpServletRequest request,
                   HttpServletResponse response, @RequestParam("file") MultipartFile file){

    ArrayList<BookInfo> list = new ArrayList<BookInfo>();
    BookInfo temp = new BookInfo();
    temp.setBookName(file.getName());
    list.add(temp);
    return list;
}

// 8
@RequestMapping("batchUpload")
@ResponseBody
public List<BookInfo> batchUpload(HttpServletRequest request,
                   HttpServletResponse response,
                   @RequestParam("files") MultipartFile[] files) throws IOException {
    ArrayList<BookInfo> list = new ArrayList<BookInfo>();
    BookInfo temp = new BookInfo();
    temp.setBookName(String.valueOf(files.length));
    list.add(temp);
    return list;
}

}