spring boot recipes第三章 spring MVC注解笔记

@RestController

      @RestController   相当于@Controller+@ResponseBody两个注解的结合,返回json数据不需要在方法前面@ResponseBody注解了,但使用这个注解,就不能返回jsp,html页面,视图解析器无法解析jsp,html页面 。

简而言之:表明当前类是控制器且返回的是一组数据,不是页面

@Controller

     @Controller  将当前修饰的类注入SpringBoot IOC容器,使得从该类所在的项目跑起来的过程中,这个类就被实例化。当然也有语义化的作用,即代表该类是充当Controller的作用 

@ResponseBody

       @ResponseBody 它的作用简短截说就是指该类中所有的API接口返回的数据,甭管你对应的方法返回Map或是其他Object,它会以Json字符串的形式返回给客户端,本人尝试了一下,如果返回的是String类型,则仍然是String。 

 @RequestMapping

@RequestMapping有8个属性。

value:指定请求的实际地址。

method:指定请求的method类型(GET,POST,PUT,DELETE)等。

consumes:指定处理请求的提交内容类型(Context-Type)。

produces:指定返回的内容类型,还可以设置返回值的字符编码。

params:指定request中必须包含某些参数值,才让该方法处理。

headers:指定request中必须包含某些指定的header值,才让该方法处理请求。

 

@GetMapping

      类似的还有

  • @GetMapping
  • @PostMapping
  • @PutMapping
  • @DeleteMapping
  • @PatchMapping

如果我们想使用传统的@RequestMapping注释实现URL处理程序,那么它应该是这样的:

@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)

新方法可以简化为:

@GetMapping(value="/get/{id}")

@PathVariable

 

@PathVariable :接收请求路径中占位符的值  

//@PathVariable可以用来映射URL中的占位符到目标方法的参数中
    @RequestMapping("/testPathVariable/{book}/{id}")
//占位符中有个id和一个book,下面的@PathVariable中的value必须一致
    public String testPathVariable(@PathVariable("id") Integer id,@PathVariable("book") String book)
    {
        System.out.println("testPathVariable:"+id);
        return "OK";
    }

ResponseEntity返回数据、状态、头部信息  

@GetMapping("/{isbn}")
public ResponseEntity<Book> get(@PathVariable("isbn") String isbn) {
  return bookService.find(isbn)
      .map(body -> ResponseEntity.ok(body))
      .orElse(ResponseEntity.notFound().build());
}

@PostMapping
public ResponseEntity<Book> create(@RequestBody Book book,
                                   UriComponentsBuilder uriBuilder) {
  Book created = bookService.create(book);
  URI newBookUri = uriBuilder.path("/books/{isbn}").build(created.getIsbn());
  return ResponseEntity
          .created(newBookUri)
          .body(created);
}

 @JsonCreator   /  @JsonProperty   

        当json在反序列化时,默认选择类的无参构造函数创建类对象,当没有无参构造函数时会报错,@JsonCreator作用就是指定反序列化时用的无参构造函数。构造方法的每个参数前面需要加上@JsonProperty,否则会报错。 

@Service 

 其getBean的默认名称是类名(头字母小写),可以@Service(“xxxx”)这样来指定;

 其定义的bean默认是单例的,可以使用@Service(“beanName”) @Scope(“prototype”)来改变;   

 可以通过@PostConstruct和@PreDestroy指定初始化方法和销毁方法(方法名任意)

//在持久层、业务层和控制层分别采用 @Repository、@Service 和 @Controller 对分层中的类进行注释,而用 @Component 对那些比较中立的类进行注释。 

 

@ModelAttribute(value="age")

一般默认值的类型(int,string)(首字母小写)作为Model中的key,value则为具体的对象,除非添加value参数,则key为设置的value参数。

 


 

    

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Solve all your Spring Boot 2 problems using complete and real-world code examples. When you start a new project, you’ll be able to copy the code and configuration files from this book, and then modify them for your needs. This can save you a great deal of work over creating a project from scratch. Using a problem-solution approach, Spring Boot 2 Recipes quickly introduces you to Pivotal's Spring Boot 2 micro-framework, then dives into code snippets on how to apply and integrate Spring Boot 2 with the Spring MVC web framework, Spring Web Sockets, and microservices. You'll also get solutions to common problems with persistence, integrating Spring Boot with batch processing, algorithmic programming via Spring Batch, and much more. Other recipes cover topics such as using and integrating Boot with Spring's enterprise services, Spring Integration, testing, monitoring and more. What You'll Learn Get reusable code recipes and snippets for the Spring Boot 2 micro-framework Discover how Spring Boot 2 integrates with other Spring APIs, tools, and frameworks Access Spring MVC and the new Spring Web Sockets for simpler web development Work with microservices for web services development and integration with your Spring Boot applications Add persistence and a data tier seamlessly to make your Spring Boot web application do more Integrate enterprise services to create a more complex Java application using Spring Boot Who This Book Is For Experienced Java and Spring programmers. Table of Contents Chapter 1: Spring Boot—Introduction Chapter 2: Spring Boot—Basics Chapter 3: Spring MVC Chapter 4: Spring MVC - Async Chapter 5: Spring WebFlux Chapter 6: Spring Security Chapter 7: Data Access Chapter 8: Java Enterprise Services Chapter 9: Messaging Chapter 10: Spring Boot Actuator Chapter 11: Packaging

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值