Springmvc注記@PathVariable@RequestHeader@CookieValue簡易テスト

6153 ワード

制御実装クラス
package org.fkit.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class DataBindingController {
    // LogFactory
    private static final Log logger = LogFactory.getLog(DataBindingController.class);

    @RequestMapping(value="/pathVariableTest/{userId}")
    public void pathVariableTest(@PathVariable Integer userId) {
        logger.info("------------------- PathVariable :"+userId);
    }

    @RequestMapping(value="/requestHeaderTest")
    public void requestHeaderTest(@RequestHeader("User-Agent") String userAgent,@RequestHeader(value="Accept") String[] accepts) {
        logger.info("-------------------- @RequestHeader :"+userAgent);
        for(String accept:accepts) {
            logger.info(accept);
        }
    }

    @RequestMapping(value="/cookieValueTest")
    public void cookieValueTest(@CookieValue(value="JSESSIONID",defaultValue="") String sessionId ) {
        logger.info("-------------------- @requestHeaderTest :"+sessionId);
    }
}

フロント
<html>
<body>
<h3> h3>
<a href="pathVariableTest/1"> @PathVariable a><br/><br/>
<a href="requestHeaderTest"> @RequestHeaderTesta><br/><br/>
<a href="cookieValueTest"> @CookieValue a><br/><br/>
body>
html>

テスト結果
 org.fkit.controller.DataBindingController pathVariableTest
 : ------------------- PathVariable :
 org.fkit.controller.DataBindingController requestHeaderTest
 : -------------------- @RequestHeader :Mozilla/5.0 (Windows NT 6.2; Win64; x64;  org.fkit.controller.DataBindingController cookieValueTest
 : -------------------- @requestHeaderTest :E95FBBC476D6EEFBDB8381A7B7D06950