Public Class Customer (Private Person Person Private Int Type
Public Class Customer (Private Person Person Private Int Type
guru/spring-framework-annotations/
https://www.w3schools.com/tags/ref_httpmethods.asp
@Required
@Autowired
@Qualifier
@Configuration
@Bean
This annotation is used at the method level. @Bean annotation works
with @Configuration to create Spring beans. As mentioned
earlier, @Configuration will have methods to instantiate and configure
dependencies. Such methods will be annotated with @Bean. The method
annotated with this annotation works as bean ID and it creates and
returns the actual bean.
Here is an example:
1. @Configuration
2. public class AppConfig{
3. @Bean
4. public Person person(){
5. return new Person(address());
6. }
7. @Bean
8. public Address address(){
9. return new Address();
10. }
@Controller
The @Controller annotation is used to indicate the class is a Spring
controller. This annotation can be used to identify controllers for
Spring MVC or Spring WebFlux.
@Service
@Repository
o @Configuration
o @EnableAutoConfiguration
o @ComponentScan
This annotation is used on Java classes that play the role of controller
in your application. The @Controller annotation allows autodetection
of component classes in the classpath and auto-registering bean
definitions for them. To enable autodetection of such annotated
controllers, you can add component scanning to your configuration.
The Java class annotated with @Controller is capable of handling
multiple request mappings.
This annotation can be used with Spring MVC and Spring WebFlux.
@RequestMapping
1. @Controller
2. @RequestMapping("/welcome")
3. public class WelcomeController{
4. @RequestMapping(method = RequestMethod.GET)
5. public String welcomeAll(){
6. return "welcome all";
7. }
8. }
In this example only GET requests to /welcome is handled by
the welcomeAll() method.
This annotation also can be used with Spring MVC and Spring
WebFlux.
@CookieValue
@CrossOrigin
This annotation is used both at class and method level to enable cross
origin requests. In many cases the host that serves JavaScript will be
different from the host that serves the data. In such a case Cross
Origin Resource Sharing (CORS) enables cross-domain
communication. To enable this communication you just need to add
the @CrossOrigin annotation.
By default the @CrossOrigin annotation allows all origin, all headers,
the HTTP methods specified in the @RequestMapping annotation
and maxAge of 30 min. You can customize the behavior by
specifying the corresponding attribute values.
An example to use @CrossOrigin at both controller and handler
method levels is this.
1. @CrossOrigin(maxAge = 3600)
2. @RestController
3. @RequestMapping("/account")
4. public class AccountController {
5.
6. @CrossOrigin(origins = "http://example.com")
7. @RequestMapping("/message")
8. public Message getMessage() {
9. // ...
10. }
11.
12. @RequestMapping("/note")
13. public Note getNote() {
14. // ...
15. }
16. }
In this example, both getExample() and getNote() methods will
have a maxAge of 3600 seconds. Also, getExample() will only
allow cross-origin requests from http://example.com ,
while getNote() will allow cross-origin requests from all hosts.
Composed @RequestMapping Variants
Spring framework 4.3 introduced the following method-level variants
of @RequestMapping annotation to better express the semantics of
the annotated methods. Using these annotations have become the
standard ways of defining the endpoints. They act as wrapper
to @RequestMapping.
These annotations can be used with Spring MVC and Spring
WebFlux.
@GetMapping
This annotation is used for mapping HTTP GET requests onto specific
handler methods. @GetMapping is a composed annotation that acts
as a shortcut for @RequestMapping(method = RequestMethod.GET)
@PostMapping
@PutMapping
This annotation is used for mapping HTTP PUT requests onto specific
handler methods. @PutMapping is a composed annotation that acts as
a shortcut for @RequestMapping(method = RequestMethod.PUT)
@PatchMapping
This annotation is used for mapping HTTP PATCH requests onto
specific handler methods. @PatchMapping is a composed annotation
that acts as a shortcut for @RequestMapping(method =
RequestMethod.PATCH)
@DeleteMapping
@ExceptionHandler
@InitBinder
@MatrixVariable
@PathVariable
@RequestAttribute
@RequestBody
@RequestParam
@RequestPart
@ResponseBody
@ControllerAdvice
@RestController
@RestControllerAdvice
@SessionAttributes
@EnableEurekaServer
This annotation is applied to Java classes. One problem that you may
encounter while decomposing your application into microservices is
that, it becomes difficult for every service to know the address of
every other service it depends on. There comes the discovery service
which is responsible for tracking the locations of all other
microservices.
Netflix’s Eureka is an implementation of a discovery server and
integration is provided by Spring Boot. Spring Boot has made it easy
to design a Eureka Server by just annotating the entry class
with @EnableEurekaServer .
@EnableDiscoveryClient
This annotation is applied on Java classes that can act as the circuit
breaker. The circuit breaker pattern can allow a micro service continue
working when a related service fails, preventing the failure from
cascading. This also gives the failed service a time to recover.
@HystrixCommand
1. @Service
2. public class BookService{
3. private final RestTemplate restTemplate;
4. public BookService(RestTemplate rest){
5. this.restTemplate = rest;
6. }
7. @HystrixCommand(fallbackMethod = "newList") public String
bookList(){
8. URI uri = URI.create("http://localhost:8081/recommended");
return this.restTemplate.getForObject(uri, String.class);
9. }
10. public String newList(){
11. return "Cloud native Java";
12. }
13. }
Here @HystrixCommand is applied to the original
method bookList() . The @HystrixCommand annotation has newList
as the fallback method. So for some reason if Hystrix opens the circuit
on bookList() , you will have a placeholder book list ready for the
users.