Hello, as a result of the rest controller operation:

@RestController @RequestMapping("/api/department") public class DepartmentRestController { @Autowired private DepartmentService departmentService; @RequestMapping(value="/byId/{id}", method=RequestMethod.GET, produces="application/json") public DepartmentDto getDepartmentById(@PathVariable Long id) { return departmentService.getDepartmentById(id); } @RequestMapping(value="/byParent/{id}", method=RequestMethod.GET, produces="application/json") public List<DepartmentDto> getDepartmentsByParent(@PathVariable Long id) { return departmentService.getDepartmentsByParent(id); } @RequestMapping(value="/byName/{filter}", method=RequestMethod.GET, produces="application/json") public List<DictionaryItem> getDepartmentsByName(@PathVariable String filter) { return departmentService.getDepartmentsByName(filter); } @RequestMapping(value="/fullTree", method=RequestMethod.GET, produces="application/json") public DepartmentDto getDepartmentsFullTree() { return departmentService.getDepartmentsFullTree(); } @RequestMapping(method=RequestMethod.POST, produces="application/json") public DepartmentDto createDepartment(@RequestBody DepartmentDto entity) { return departmentService.createDepartment(entity); } @RequestMapping(method=RequestMethod.PUT) public void updateDepartment(@RequestBody DepartmentDto entity) { departmentService.updateDepartment(entity); } @RequestMapping(value="/byId/{id}", method=RequestMethod.DELETE) public void deleteDepartmentsByIds(@PathVariable Long id) { departmentService.deleteDepartment(id); } } 

It turns out this information:

 {"id":1, "login":"ivanenko.ivanovich", "password":"example", "role":null, "lastName":"Иваненко", "firstName":"Иван", "middleName":"Иванович", "department":null, "email":null, "phone":"0954533452", "description":"Хороший студент", "testResults":null} 

How to use this information in java? Tell me please.

  • Как при помощи javascript обработать ..... Как пользоваться этой информацией в java? ....... so where exactly do you want to do something about it? - Alexey Shimansky
  • I want to create a javascript object from received information from java - user212179
  • 3
    @ user212179 as I see it, you get JSON from the server. The simplest option is to use the JSON.parse () function - which will return the object to you based on the transferred JSON string. - RussCoder
  • @enzo, please make your comment as an answer. - Nofate

2 answers 2

If you send a request to the server using any JS library (as it usually happens), for example, JQuery, then you do n’t need to parse anything at all. The type of response is determined by the function itself or the request parameter. What does this mean? As an example, look at the description of the jQuery getJSON() function. The documentation says:

This is a shorthand Ajax function, which is equivalent to:

 $.ajax({ dataType: "json", url: url, data: data, success: success }); 

Those. just an abbreviation for the standard AJAX request function with a predefined dataType parameter dataType . Thus, you immediately say that the server returns data in JSON format. Your JS library, knowing this, will automatically parse JSON and provide a ready-made JS object.

This is a javascript structure and parsed using the $ .parseJSON () method .

Those. At the exit of the request in success callback, you should have a ready-made JS object. The simplest example of a query:

Jquery

HTML

 <span id="login"></span> 

Js

 $.getJSON( "/api/department/byId/1", function(data) { console.log(data.login); $('#login').text(data.login); }); 

Angular

HTML

 <span>{{department.login}}</span> 

Js

 $http.get("/api/department/byId/1").then(function(response) { console.log(response.data); $scope.department = response.data; }); 

Your controller can, in principle, be removed from the question - the client code is more important for the answer

    You must create a DTO model that will accept your JSON format in a particular controller method. To do this, you need to create an object with names that will completely match the fields in Json and accept it in the method parameter using the @RequestBody attribute @RequestBody but for this you need to designate the method that accepts JSON with the @ResponseBody attribute. After that, all incoming information in JSON format you automatically translate into a java object and then you can operate with it as you wish. Set it in entiti in the database or do any other operations on this object.

    Example:

     @RequestMapping(value = "/saveDish", method = RequestMethod.POST, consumes = "application/json") public @ResponseBody String saveDish(@RequestBody DishDto dish) { ... } 

    Here you accept the usual json format, which is parsed at the annotation level into a Java object.

    If, on the client side, you need to process json, which is returned by the server, then use Ajax and jQuery . Read it with the .each function. In the ajax request, parse it into the blocks you need on the page and use it as a normal loop, the elements of which can also be accessed by fields.