What happens when we return a bean from a request

Info icon This preview shows pages 281–297. Sign up to view the full content.

View Full Document Right Arrow Icon
What happens when we return a bean from a Request Mapping Method? What is GetMapping and what are the related methods available in Spring MVC?
Image of page 281

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
@GetMapping ( "/users" ) public List<User> retrieveAllUsers () { return service.findAll(); }
Image of page 282
Questions Can you show the code for an example Post Resource method with Spring REST? Why do we use ResponseEntity in a RESTful Service?
Image of page 283

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
@PostMapping ( "/users" ) public ResponseEntity<Object> createUser ( @Valid @RequestBody User user) { User savedUser = service.save(user); URI location = ServletUriComponentsBuilder .fromCurrentRequest() .path( "/{id}" ) .buildAndExpand(savedUser.getId()).toUri(); return ResponseEntity.created(location).build(); }
Image of page 284
Questions What is HATEOAS? Can you give an Example Response for HATEOAS? How do we implement it using Spring?
Image of page 285

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
HATEOAS Hypermedia as The Engine of Application State Example When requested for details of a facebook post, we return Link for actions to like, unlike or comment on the post
Image of page 286
{ " id ": 1, " name ": "Adam" , " birthDate ": "2017-07-19T09:26:18.337+0000" , " _links ": { " all-users ": { " href ": " " } } }
Image of page 287

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
<dependency> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-starter-hateoas </artifactId> </dependency>
Image of page 288
@GetMapping ( "/users/{id}" ) public Resource<User> retrieveUser (@PathVariable int id) { User user = service.findOne(id); Resource<User> resource = new Resource<User>(user); ControllerLinkBuilder linkTo = linkTo(methodOn( this .getClass()).retrieveAllUsers()); resource.add(linkTo.withRel( "all-users" )); return resource; }
Image of page 289

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Questions How do you document RESTful web services? Can you give a brief idea about Swagger Documentation? How do you automate generation of Swagger Documentation from RESTful Web Services? How do you add custom information to Swagger Documentation generated from RESTful Web Services? What is Swagger-UI?
Image of page 290
OpenAPI Specification (formerly called the Swagger Specification). The specification creates the RESTful contract for your API, detailing all of its resources and operations in a human and machine readable format for easy development, discovery, and integration.
Image of page 291

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
<dependency> <groupId> io.springfox </groupId> <artifactId> springfox-swagger2 </artifactId> <version> 2.4.0 </version> </dependency> <dependency> <groupId> io.springfox </groupId> <artifactId> springfox-swagger-ui </artifactId> <version> 2.4.0 </version> </dependency>
Image of page 292
@Configuration @EnableSwagger 2 public class SwaggerConfig { @Bean public Docket api () { return new Docket(DocumentationType.SWAGGER_2); } }
Image of page 293

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
public static final Contact DEFAULT_CONTACT = new Contact( "Ranga Karanam" , " " , "in28minute public static final ApiInfo DEFAULT_API_INFO = new ApiInfo( "Awesome API Title" , "Awesome API Description" , "1.0" , "urn:tos" , DEFAULT_CONTACT, "Apache 2.0" , " " private static final Set<String> DEFAULT_PRODUCES_AND_CONSUMES new HashSet<String>(Arrays.asList( "application/json" , "application/xml" ));
Image of page 294
@Bean public Docket api () { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(DEFAULT_API_INFO) .produces(DEFAULT_PRODUCES_AND_CONSUMES) .consumes(DEFAULT_PRODUCES_AND_CONSUMES); }
Image of page 295

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Customizing @ApiModel (description= "All details about the user." ) @Entity public class User { @Size (min=2, message= "Name should have atleast 2 characters" @ApiModelProperty (notes= "Name should have atleast 2 characte private String name;
Image of page 296
Image of page 297
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern