当前位置: 首页 > news >正文

做调查报告的网站如何做免费网站推广

做调查报告的网站,如何做免费网站推广,网站百度地图导航代码生成,wordpress昨英文1. 常用类注解 RestController和Controller是Spring中用于定义控制器的两个类注解. 1.1 RestController RestController是一个组合类注解,是Controller和ResponseBody两个注解的组合,在使 用 RestController 注解标记的类中,每个方法的返回值都会以 JSON 或 XML…

1. 常用类注解

@RestController和@Controller是Spring中用于定义控制器的两个类注解.

1.1 @RestController

@RestController是一个组合类注解,是@Controller@ResponseBody两个注解的组合,在使

用 @RestController 注解标记的类中,每个方法的返回值都会以 JSON 或 XML等 的形式直接写入 HTTP 响应体中,相当于在每个方法上都添加了 @ResponseBody注解.

代码演示(访问127.0.0.1:8080/hello/hello1):

@RequestMapping("/hello")
@RestController
public class HelloController {@RequestMapping("/hello1")public String hello() {return "hello spring mvc1";}}

返回的结果如下:

我们可以使用fiddler进行抓包:

可以看到返回的类型是text/html,可以把它看成是一个text或者是html.

1.2 @Controller

@Controller是一个类注解,其主要作用是标记类表示返回的是一个视图(view).这是用于传统的SpringMVC架构中的类注解.

代码演示:

@RequestMapping("/response")
@Controller
public class ResponseController {@RequestMapping("/index")public String index(){return "/index.html";}}
//index.html
<!doctype html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport"content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>用户登录首页</title>
</head><body>登录人: <span id="loginUser"></span><script src="/jquery.min.js"></script><script></script>
</body></html>

这样就返回了一个html页面(视图).

总之,@RestController和@Controller的区别就在于要返回一个视图(页面)还是要返回数据.

1.3 @RequestMapping

@RequestMapping表示路由映射,即通过@RequestMapping注解可以达到设置url的目的.在访问时只有加入@RequestMapping映射的部分,才能成功找到这个路径.\

代码演示:

@RequestMapping("/hello")
@RestController
public class HelloController {@RequestMapping("/hello1")public String hello() {return "hello spring mvc1";}
}

我们只有启动服务器,再访问127.0.0.1:8080/hello/hello1才能成功接收到后端返回的"hello spring mvc1"这个数据.

 2. 常用方法注解

2.1 @RequestMapping

我们刚才介绍了@RequestMapping是一个类注解,它其实还是一个方法注解,我们在使用方法时需要在方法上也添加上这个注解.同样表示的是路由映射,影响我们访问的url.我们再来看一下,其中的属性:

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
@Reflective({ControllerMappingReflectiveProcessor.class})
public @interface RequestMapping {String name() default "";@AliasFor("path")String[] value() default {};@AliasFor("value")String[] path() default {};RequestMethod[] method() default {};String[] params() default {};String[] headers() default {};String[] consumes() default {};String[] produces() default {};
}
public enum RequestMethod {GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS,TRACE;
}

其中我们主要介绍method这个属性,method取值于RequestMethod这个枚举类型,我们使用的最多的就是GET和POST:

    @GetMapping("/hello3")public String hello3() {return "hello spring mvc3";}@PostMapping("/hello4")public String hello4() {return "hello spring mvc4";}

报错信息:Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' is not supported]

POST表示只有使用post请求时才允许进行正确的响应,GET表示get请求时才允许进行正确的响应.如果这个参数不写,就表示post和get请求都允许.

2.2 @PostMapping

与 @RequestMapping(value = "/hello2",method = RequestMethod.POST)的效果类似,只允许post请求.

2.3 @GetMapping

与 @RequestMapping(value = "/hello2",method = RequestMethod.GET)的效果类似,只允许get请求.

2.4 @RequestParam

这个参数主要有两个作用.

在前端给后端传递参数时,需要规定好参数的名称,使其保持一致否则将无法成功传参.例如前端传给我们的用户名称叫做userName,但是后端接收这个参数的名称叫做name,就无法成功接收.但是我们可以使用@RequestParam这个注解进行参数的绑定,将userName绑定到name上,这样后端就可以使用name进行操作了.还可以在其中required属性中设置true/false,表示这个参数是否是必须的.

代码演示:

    @RequestMapping("/param6")public String param6(@RequestParam(value = "userName",required = false)String name,String password){System.out.println("receive userName:"+name+" password:"+ password);return "receive name: "+name+" password:"+ password;}

另外一个主要作用是用来传递集合.对于一个简单的数组来说,我们直接传递不会出现任何问题,但是如果我们直接传递一个集合,就会发现传过来的集合是空的.这时,就需要@RequestParam这个注解来将数组绑定到这个集合上,给集合赋值.'

代码演示:

    @RequestMapping("/param8")public String param8(@RequestParam("listParam") List<String> listParam){System.out.println("receive listParam: "+listParam);return "receive listParam: "+listParam;}

2.5 @RequestBody

@RequestBody这个注解主要用来接收对象(传递JSON类型的参数),比如我们需要给后端传递一个UserInfo对象:

public class UserInfo {public String userName;public String password;public int age;@Overridepublic String toString() {return "UserInfo{" +"userName='" + userName + '\'' +", password='" + password + '\'' +", age=" + age +'}';}
}

我们就需要给出userName,password和age三个参数的值,在传递过程中会将这三个参数转变成JSON格式的数据,只有我们加上@RequestBody这个注解才能成功接收到这个JSON数据,并把它转换成对象.

代码演示:

    @RequestMapping("/param9")public String param9(@RequestBody UserInfo userInfo){System.out.println("receive userInfo: "+userInfo);return "receive userInfo: "+userInfo;}

2.6 @PathVariable

@PathVariable这个注解用来从url中获取到参数.比如下述例子,我们要从url中获取到articlName,我们就可以使用这个注解加上一定的格式进行获取:

    @RequestMapping("/param10/{articlName}")public String param10(@PathVariable("articlName") Integer articlName){System.out.println("receive articlName: "+articlName);return "receive articlName: "+articlName;}

2.7 @RequestPart

@PathVariable这个注解用来进行文件的接收.代码演示如下:

@RequestMapping("/param12")public String param12(@RequestPart MultipartFile file) throws IOException {System.out.println(file.getName());System.out.println(file.getOriginalFilename());System.out.println(file.getContentType());file.transferTo(new File("D:/temp/"+file1.getOriginalFilename()));return file1.getOriginalFilename();}

2.8 @CookieValue

@CookieValue这个注解用来根据cookie中的键获取对应的值.代码演示如下:

    @RequestMapping("/getCookie2")public String getCookie2(@CookieValue("lzq") String value) {System.out.println(value);System.out.println("get cookies successfully");return "get cookies successfully";}

2.9 @SessionAttribute

@SessionAttribute这个注解用来根据session中的键获取对应的值.代码演示如下:

    @RequestMapping("/getSession3")public String getSession3(@SessionAttribute(value = "name",required = false) String name) {System.out.println("name "+name);System.out.println("get session successfully");return "get session successfully";}

2.10 @RequestHeader

@RequestHeader这个注解用来根据header中的键获取对应的值.代码演示如下:

    @RequestMapping("/getHeader2")public String getHeader2(@RequestHeader("User-Agent") String userAgent) {System.out.println(userAgent);System.out.println("get userAgent successfully");return "get userAgent successfully";}

2.11 @ResponseBody

@ResponseBody是@RestController的一部分,通常用在当@Controller修饰类控制器,而其中某些方法需要返回数据而不是视图时,代码演示如下:

    @ResponseBody@RequestMapping("/returnData")public String returnData() {System.out.println("returnData");return "returnData";}

http://www.15wanjia.com/news/34557.html

相关文章:

  • 东方头条网站源码seo优化关键词排名优化
  • 上海做公益活动有哪些好的网站滨州网站建设
  • 怎么样让客户做网站和小程序seo网站页面优化包含
  • 苹果cms永久免费最新seo网站优化教程
  • 直销软件开发 大黄蜂太原seo排名收费
  • 网站推广主要怎么做成功的软文推广
  • 自己做网站项目网站关键词排名外包
  • 创建网站的价格优化网络的软件
  • 凡科建设网站入门版好不360推广开户
  • 网站建设与管理实训课程站长工具seo排名
  • 网络游戏服务网天津seo排名效果好
  • 帝国cms做投资网站源码企业网络营销策划书
  • 旅游网站设计策划书低价刷粉网站推广
  • flash网站优化中山疫情最新消息
  • 搜索引擎优化排名seoseo软件简单易排名稳定
  • php 优化网站建设域名解析查询工具
  • 用源码做网站外包网络推广公司推广网站
  • 东莞网站快速优化排名59软文网
  • asp.net做织梦网站制作网页一般多少钱
  • 南京移动网站建设百度推广官网首页
  • 新浪云sae免费wordpress网站推广网站源码
  • 如何设置网站icon磁力搜索引擎torrentkitty
  • 17网站一起做网店池尾商圈站长工具中文
  • 网站建设 环保 图片友情链接检查
  • 企业服务类型有哪些重庆快速排名优化
  • 做时时彩网站合法的吗购物网站哪个最好
  • php成品网站游戏推广一个月能拿多少钱
  • 哪个免费建站好百度快照入口
  • 做网站推广邢台营销推广技巧
  • 石家庄做外贸的网站建设厦门seo管理