Java shares the @responseBody annotation for SpringMVC. In this chapter, we learn how the server side of SpringMVC provides the data query service. We need to use two important annotations: @responseBody and @restController.

@ ResponseBody annotations

In the previous use of SpringMVC, the method return value in the Controller was processed by the view handler, ViewResolver, as the URL of the page, and then jumped to the corresponding page. As in the following example, the return value of Hello was converted to /WEB-INF/pages/hello.jsp

@controller public class UserController{@requestMapping (" /login ") public Stringlogin() {return"Hello". }}Copy the code

  

Sometimes we need to use Controller to realize network service interface, such as commodity query, weather query, news data and so on. You don’t need to jump to the page and return the data directly.

And at this point we can add a comment to this method: @responseBody

@controller public class UserController{@responseBody @requestMapping (" /login ") public Stringlogin() {return"Hello". }}Copy the code

  

  

Display hello text directly in the browser, that is, add the @responsebody annotation method, and the return value is sent directly to the browser via the HTTP ResponseBody.

Type converter

By default, @responseBody returns only String data.

  

You can add a converter to convert data to a specific format, such as XML or JSON. Json is the main format for data communication. We will add a CONVERTER in JSON format. The configuration method is very simple, we just need to add the jackson-Databind dependency:

< the dependency > < groupId > com. Fasterxml. Jackson. Core < / groupId > < artifactId > Jackson - databind < / artifactId > The < version > 2.8.7 < / version > < / dependency >Copy the code

  

The Jackson library converts Java objects to JSON, and SpringMVC automatically adds Jackson converters when jackson-Databind dependencies are introduced.

Convert custom types

Let’s test again, adding a method test that returns Boolean data

@ ResponseBody @ RequestMapping ("/login2") public Booleanlogin2() {return false; }Copy the code

  



When we perform data query, we need to return data of custom type, such as: user, commodity, order, article, etc. Here we test to return custom type:

Public class User {private int id; Private String username; Private String birthday; Private String sex; Private String address; / / add the get \set\ Construction methodCopy the code

  

.

} @ ResponseBody @ RequestMapping ("/login3") public Userlogin3(){User User = new User(1,"Zhang"."1990-2-1"."Male"."Wuhan");returnuser; }Copy the code

  



  

You can see that the User object has been converted to JSON format. Retest object collection:

@ ResponseBody @ RequestMapping ("/login4")

      public List<User> login4(){List<User> users = new ArrayList<>(); users.add(new User(1,"Zhang"."1990-2-1"."Male"."Wuhan")); users.add(new User(2,"Bill"."1990-2-1"."Male"."Wuhan")); users.add(new User(3,"Fifty"."1990-2-1"."Male"."Wuhan"));returnThe users; }Copy the code

  

  

The List is converted into a JSON array. When we query the data from the database, we put it into the corresponding entity class. Then we convert the collection into A JSON format and send it to the client.

@ RestController annotations

If a Controller class is primarily used for web services, and all of our methods need the @responseBody annotation, we can put the @RestController annotation at the front of the class, which is equivalent to @controller + @responseBody, That is, it can be used either to declare a class Controller or to automatically annotate all methods with @responseBody.

  

conclusion

In this chapter, we learned about the @responseBody annotation in SpringMVC, a method for returning data directly to the client, and how to convert custom types to JSON format using converters provided by the Jackson library, which are often needed in real projects to provide services for querying data.