📜 ⬆️ ⬇️

ContactManager, part 4. Add a web service (REST)

The ink on the previous version of the ContactManager application did not have time to dry out , as the phone rang, and I heard the voice of a friend in the tube, who began to master the development for Android and was looking for a test project on which he could practice working with web services.

“Nothing is easier!” I replied.

So what do we have at the moment?
A web application that runs in a browser, for authorization, the user must enter a username and password. The controller manages views that display specific JSP pages to the user.

In the case of a web service, there is no login form, no JSP pages. One solid HTTP: data is sent in requests and returned back as JSON (as an option, XML).
')
Work plan:


"Straight on points and let's go" (C).

1. Add security settings.

Security for a web service can be implemented using Basic Authentication . Let's see how Spring supports this mechanism.

To begin with we will be defined with a format of requests. We already have a set of URLs for working with the browser, we can take it as a sample. Just “hide” URLs for the web service behind the mnemonic prefix " /ws ". That is, we must handle the following set of addresses: /ws/index , /ws/add , /ws/delete . Access to the root " /ws " we voluntarily decide to ban, because there is no need.

Spring allows you to specify multiple http elements in a single security file. Take advantage of this. Add to the beginning:
  <http realm="Contact Manager REST-service" pattern="/ws/**" use-expressions="true"> <intercept-url pattern="/ws/index*" access="hasAnyRole('ROLE_USER','ROLE_ANONYMOUS')" /> <intercept-url pattern="/ws/add*" access="hasRole('ROLE_USER')" /> <intercept-url pattern="/ws/delete/*" access="hasRole('ROLE_ADMIN')" /> <intercept-url pattern="/ws/**" access="denyAll" /> <http-basic/> </http> 

It is important to add this section to the beginning, before the existing settings. Since it is more specific, it must be processed first in order to intercept the request with the /ws prefix in a timely manner.

What changed? Very little. Everything related to JSP has disappeared, checking roles now uses the expression mechanism use-expressions="true" (but this is not important, just as an illustration). Perlfix /ws added to all existing urls, access to the /ws root is denied for all ( denyAll ). Examples of the use of other SPEL expressions can be found here . I repeat - the order of specifying masks is important, the most common /ws/** the last. The realm attribute is added again for beauty, it will be displayed in the authorization window, if someone decides to admire the JSON contact list through the browser.

And further the invisible element <http-basic/> already sheltered. Behind this short construction, Spring (in its usual style) hides from the developer the complexity of the mechanism of this Basic Authentication itself. And we have no reason not to trust him in this. One down, two to go. Let's do the controller.
NB We must not forget that we have 2 security.xml files, the main one for tests. Changes need to be made in both.

2. Add a controller.

How do we turn a controller for a JSP into a controller for a web service? Very simple. Existing methods return a String with the name view, or with a forward / redirect page, while the data (lists of contracts and their types) are passed in the attributes of the model. Web service methods must return objects suitable for serialization in JSON. Plus, the POST method /ws/add will accept the data of the new contact in the form of a JSON string directly in the request body. It's time to work.

Create a new ContactWsController.java file in the same package as the old controller. And immediately at the class level, we denote our claims that this is a web service.
 @Controller @RequestMapping(value = "/ws", produces = MediaType.APPLICATION_JSON_VALUE) public class ContactWsController { @Autowired private ContactService contactService; } 

@RequestMapping(value="/ws") at the class level sets a common prefix that will be automatically added to all URLs of individual methods. produces = MediaType.APPLICATION_JSON_VALUE says that by default all methods of this controller will render JSON. If necessary, in a specific method, this value can be redefined.
By tradition, start with a list of contacts. The old method looks like this:
  @RequestMapping("/index") public String listContacts(Map<String, Object> map) { map.put("contact", new Contact()); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return "contact"; } 

In order not to make a DTO for each method, let 's accept the agreement that the structure of the returned JSON will be a Map. We remove the method parameter, we do not need it, change the type of the return value to Map and add the @ResponseBody annotation to the return value. It tells Spring that we want this value to be serialized in JSON and written to the response body. The new method looks like this:
  @RequestMapping(value = "/index") @ResponseBody public Map<String, Object> listContacts() { Map<String, Object> map = new HashMap<String, Object>(); map.put("contact", new Contact()); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } 


In order not to postpone the matter indefinitely and quickly see the fruits of our efforts, let us step back a little from our plan and immediately try to test this method.

2.1 First test method

But it is important to understand exactly what results we have to wait in the test. Make a new class MockMvcWsTest.groovy , copy into it all the main stuffing related to setting up MockMvc. And copy the old test:
  @Test public void index_user1() { mockMvc.perform(MockMvcRequestBuilders.get("/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andExpect(MockMvcResultMatchers.view().name("contact")) .andExpect(MockMvcResultMatchers.model().attributeExists("contact")) .andExpect(MockMvcResultMatchers.model().attributeExists("contactList")) .andExpect(MockMvcResultMatchers.model().attributeExists("contactTypeList")) } 

All data come in models. But in the web service we were going to send them in the request body. Maybe there is a search for them? Let's try.
  @Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andReturn() //      -   JSON println result.response.contentAsString } 

We start. The test is completed, but there is nothing in the console. Hm We'll have to podobezhit, set the breakpoint in the controller, run. Yeah, it enters the controller. But back something useful does not come. We look at the value of the result - that is, in resolvedException stands HttpMediaTypeNotAcceptableException , and mockResponse.status = 406. The diagnosis is still disappointing “Could not find acceptable representation”.

But on the other hand, we asked to turn the Map into JSON, and we didn’t say who will do it and how. Spring, of course, can do a lot, but not everything. Examining the documentation gives us the following result:

Hmm, the presence of two configurations is definitely starting to cause inconvenience, but for now we will not be distracted by this. We start the test, we see the required line. But ... instead of Cyrillic krakozybly.
 {"contactTypeList":[{"id":1,"code":"family","name":"Ð¡ÐµÐ¼ÑŒÑ ","defaulttype":false,"contacts":null},{"id":2,"code":"job","name":"Работа","defaulttype":false,"contacts":null},{"id":3,"code":"stuff","name":"Знакомые","defaulttype":true,"contacts":null}],"contactList":[],"contact":{"id":null,"firstname":null,"lastname":null,"email":null,"telephone":null,"contacttype":null}} 

Again, we google, smoke manuals and source codes and find the following solution: add a charset to @ RequestMapping.produces (to the one that is at the class level).
 @RequestMapping(value = "/ws", produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8" ) 

It does not look like a fountain, those who wish can search for a “more different” option, we will stop there.

Another couple of comments

Remark 1 . Understanding what happens inside the test will facilitate the construction of .andDo(MockMvcResultHandlers.print()) It displays detailed information in the log. For example, for our request the log will look like this.
 MockHttpServletRequest: HTTP Method = GET Request URI = /ws/index Parameters = {} Headers = {} Handler: Type = net.schastny.contactmanager.web.ContactWsController Method = public java.util.Map<java.lang.String, java.lang.Object> net.schastny.contactmanager.web.ContactWsController.listContacts() Async: Was async started = false Async result = null Resolved Exception: Type = null ModelAndView: View name = null View = null Model = null FlashMap: MockHttpServletResponse: Status = 200 Error message = null Headers = {Content-Type=[application/json;charset=UTF-8]} Content type = application/json;charset=UTF-8 Body = {"contactTypeList":[{"id":1,"code":"family","name":"","defaulttype":false,"contacts":null},{"id":2,"code":"job","name":"","defaulttype":false,"contacts":null},{"id":3,"code":"stuff","name":"","defaulttype":true,"contacts":null}],"contactList":[],"contact":{"id":null,"firstname":null,"lastname":null,"email":null,"telephone":null,"contacttype":null}} Forwarded URL = null Redirected URL = null Cookies = [] 

Much better, isn't it?

Remark 2 . You can see that the ContactType class is serialized by default to JSON along with all the attributes. But the serialization of the List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null

, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin() index_na() .

2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody , JSON- ( @RequestBody String json ) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping consumes = MediaType.TEXT_PLAIN_VALUE , "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }

, REST- . - . Basic Authentication , . HTTPS - .

.

GitHub
attribute is List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null

, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin() index_na() .

2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody , JSON- ( @RequestBody String json ) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping consumes = MediaType.TEXT_PLAIN_VALUE , "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }

, REST- . - . Basic Authentication , . HTTPS - .

.

GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null

, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin() index_na() .

2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody , JSON- ( @RequestBody String json ) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping consumes = MediaType.TEXT_PLAIN_VALUE , "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }

, REST- . - . Basic Authentication , . HTTPS - .

.

GitHub
 List contacts = null  ,    -     No Session,       .   -   " "      @JsonIgnore 
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null

, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin() index_na() .

2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody , JSON- ( @RequestBody String json ) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping consumes = MediaType.TEXT_PLAIN_VALUE , "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }

, REST- . - . Basic Authentication , . HTTPS - .

.

GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null

, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin() index_na() .

2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody , JSON- ( @RequestBody String json ) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping consumes = MediaType.TEXT_PLAIN_VALUE , "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }

, REST- . - . Basic Authentication , . HTTPS - .

.

GitHub
 List contacts = null  ,    -     No Session,       .   -   " "      @JsonIgnore 
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null

, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin() index_na() .

2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody , JSON- ( @RequestBody String json ) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping consumes = MediaType.TEXT_PLAIN_VALUE , "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }

, REST- . - . Basic Authentication , . HTTPS - .

.

GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null

, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin() index_na() .

2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody , JSON- ( @RequestBody String json ) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping consumes = MediaType.TEXT_PLAIN_VALUE , "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }

, REST- . - . Basic Authentication , . HTTPS - .

.

GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null

, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin() index_na() .

2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody , JSON- ( @RequestBody String json ) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping consumes = MediaType.TEXT_PLAIN_VALUE , "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }

, REST- . - . Basic Authentication , . HTTPS - .

.

GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null

, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin() index_na() .

2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody , JSON- ( @RequestBody String json ) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping consumes = MediaType.TEXT_PLAIN_VALUE , "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }

, REST- . - . Basic Authentication , . HTTPS - .

.

GitHub
 List contacts = null  ,    -     No Session,       .   -   " "      @JsonIgnore 
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null

, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin() index_na() .

2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody , JSON- ( @RequestBody String json ) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping consumes = MediaType.TEXT_PLAIN_VALUE , "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }

, REST- . - . Basic Authentication , . HTTPS - .

.

GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null

, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin() index_na() .

2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody , JSON- ( @RequestBody String json ) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping consumes = MediaType.TEXT_PLAIN_VALUE , "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }

, REST- . - . Basic Authentication , . HTTPS - .

.

GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null

, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin() index_na() .

2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody , JSON- ( @RequestBody String json ) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping consumes = MediaType.TEXT_PLAIN_VALUE , "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }

, REST- . - . Basic Authentication , . HTTPS - .

.

GitHub
 List contacts = null  ,    -     No Session,       .   -   " "      @JsonIgnore 
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null

, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin() index_na() .

2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody , JSON- ( @RequestBody String json ) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping consumes = MediaType.TEXT_PLAIN_VALUE , "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }

, REST- . - . Basic Authentication , . HTTPS - .

.

GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null

, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin() index_na() .

2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody , JSON- ( @RequestBody String json ) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping consumes = MediaType.TEXT_PLAIN_VALUE , "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }

, REST- . - . Basic Authentication , . HTTPS - .

.

GitHub
 List contacts = null  ,    -     No Session,       .   -   " "      @JsonIgnore 
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null

, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin() index_na() .

2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody , JSON- ( @RequestBody String json ) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping consumes = MediaType.TEXT_PLAIN_VALUE , "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }

, REST- . - . Basic Authentication , . HTTPS - .

.

GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null

, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin() index_na() .

2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody , JSON- ( @RequestBody String json ) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping consumes = MediaType.TEXT_PLAIN_VALUE , "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }

, REST- . - . Basic Authentication , . HTTPS - .

.

GitHub
 List contacts = null  ,    -     No Session,       .   -   " "      @JsonIgnore 
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null

, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin() index_na() .

2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody , JSON- ( @RequestBody String json ) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping consumes = MediaType.TEXT_PLAIN_VALUE , "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }

, REST- . - . Basic Authentication , . HTTPS - .

.

GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null

, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin() index_na() .

2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody , JSON- ( @RequestBody String json ) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping consumes = MediaType.TEXT_PLAIN_VALUE , "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }

, REST- . - . Basic Authentication , . HTTPS - .

.

GitHub

Source: https://habr.com/ru/post/173593/


All Articles