spring requestmapping shortcuts (#1096)

* rest with spark java

* 4

* Update Application.java

* indentation changes

* spring @requestmapping shortcuts

* removing spring requestmapping and pushing spring-mvc-java
This commit is contained in:
Abhinab Kanrar 2017-02-05 02:46:35 +05:30 committed by maibin
parent 791142c67e
commit b57cf0cf56
3 changed files with 143 additions and 3 deletions

View File

@ -6,9 +6,10 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@ -0,0 +1,47 @@
package com.baeldung.web.controller;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RequestMappingShortcutsController {
@GetMapping("/get")
public @ResponseBody ResponseEntity<String> get() {
return new ResponseEntity<String>("GET Response", HttpStatus.OK);
}
@GetMapping("/get/{id}")
public @ResponseBody ResponseEntity<String> getById(@PathVariable String id) {
return new ResponseEntity<String>("GET Response : " + id, HttpStatus.OK);
}
@PostMapping("/post")
public @ResponseBody ResponseEntity<String> post() {
return new ResponseEntity<String>("POST Response", HttpStatus.OK);
}
@PutMapping("/put")
public @ResponseBody ResponseEntity<String> put() {
return new ResponseEntity<String>("PUT Response", HttpStatus.OK);
}
@DeleteMapping("/delete")
public @ResponseBody ResponseEntity<String> delete() {
return new ResponseEntity<String>("DELETE Response", HttpStatus.OK);
}
@PatchMapping("/patch")
public @ResponseBody ResponseEntity<String> patch() {
return new ResponseEntity<String>("PATCH Response", HttpStatus.OK);
}
}

View File

@ -0,0 +1,92 @@
package com.baeldung.web.controller;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import com.baeldung.spring.web.config.WebConfig;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = WebConfig.class)
public class RequestMapingShortcutsUnitTest {
@Autowired
private WebApplicationContext ctx;
private MockMvc mockMvc;
@Before
public void setup () {
DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(this.ctx);
this.mockMvc = builder.build();
}
@Test
public void giventUrl_whenGetRequest_thenFindGetResponse() throws Exception {
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/get");
ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("GET Response");
this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk());
}
@Test
public void giventUrl_whenPostRequest_thenFindPostResponse() throws Exception {
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post("/post");
ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("POST Response");
this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk());
}
@Test
public void giventUrl_whenPutRequest_thenFindPutResponse() throws Exception {
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.put("/put");
ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("PUT Response");
this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk());
}
@Test
public void giventUrl_whenDeleteRequest_thenFindDeleteResponse() throws Exception {
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.delete("/delete");
ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("DELETE Response");
this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk());
}
@Test
public void giventUrl_whenPatchRequest_thenFindPatchResponse() throws Exception {
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.patch("/patch");
ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("PATCH Response");
this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk());
}
}