spring-boot Web Development Fundamentals 1- comment section

A, @ GetMapping @PostMapping @PutMapping etc.

Two, @ RequestParam pass parameters

            value- specified parameter name defaultValue: the default value, optional required: Are Required

   /**
     * @GetMapping:Spring4.3新特性,只支持get请求
     * @PostMapping,@PutMapping同理
     * @RequestParam:传递参数:value:指定参数名字 defaultValue:默认值,可选
     * required:是否必须
     */ 
@ResponseBody
    @GetMapping("/getmapping")
    public String get(@RequestParam(value = "username", defaultValue = "houzheng") String usrename,
                      @RequestParam(value = "password", required = true) String password) {
        return "get springboot" + usrename + "---" + password;

    }

 Three, @ PathParam acquisition parameters in the url

   /**
     * @PathParam:获取url中的参数 , 可直接注入servlet的api
     */
    @ResponseBody
    @PostMapping("/postmapping/{id}")
    public String post(@PathParam("id") String id, HttpServletRequest req) {
        req.getRemoteHost();
        return "post springboot";
    }

Four, @ RestController

        For the class representation body may be directly output without write @ResponseBody

        Although the results are output as json, but if you do not set the Content-Type, the default is html.

        Setting Role:

        MIME Content-Type entity header is used to indicate the type of resource media type. In response, Content-Type header tells the client the actual content type of the returned content. The browser will be, in some cases MIME look, do not necessarily follow the value of this title; To prevent this behavior, the title X-Content-Type-Options can be set to nosniff. In the request (such as POST or PUT), the client tells the server type of data actually transmitted.


     

      Spring left a setup response message Content-Type interface. In the annotation @RequestMapping increase produces a parameter keys to.

       @RequestMapping(value = { "/api/v1/test" }, method = { RequestMethod.POST }, produces="application/json;charset=UTF-8")

        Provided corresponding to the code

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");

 

Guess you like

Origin blog.csdn.net/lidongliangzhicai/article/details/91490618