Web development method development

The development steps of the web development mode and the JAVA application mode are similar.

This project [code example] (https://github.com/huaweicse/servicecomb-java-chassis-samples/tree/master/spring-boot-web)

Mainly differences:

  • JAVA application is based on spring-boot-starter, and web development is based on spring-boot-starter-web.

  • JAVA application depends on spring-boot-starter-provider, while web development depends on spring-boot-starter-transport. Spring-boot-starter-web already carries hibernate-validator and does not require additional dependencies.

  • In the startup function, the web development mode can be declared

@SpringBootApplication(exclude=DispatcherServletAutoConfiguration.class)

To close org.springframework.web.servlet.DispatcherServlet, enable org.apache.servicecomb.transport.rest.servlet.RestServlet via @EnableServiceComb. Although it is not necessary to exclude the DispatcherServlet, it is not a good idea to have multiple REST frameworks in a microservice in most scenarios, which will cause much confusion in use.

  • Specify the URL root path of the RestServlet in the microservice.yaml file via the configuration item servicecomb.rest.servlet.urlPattern. And the listening port in the configuration item servicecomb.rest.address must be consistent with the port that the tomcat listens on (the default is 8080, which can be modified by adding server.port in application.yml)

After integrating java chassis, you can develop REST interface through it:

@RestSchema(schemaId = "hello")
@RequestMapping(path = "/")
public class HelloService {
    @RequestMapping(path = "hello", method = RequestMethod.GET)
    public String sayHello(@RequestParam(name="name") String name) {
        return "Hello " + name;
    }
}

Then you can access it by http://localhost:9093/hello?name=world.

You can see that the tags used are mostly the same as Spring MVC. But there are also a few different places, such as:

  1. Replace RestController with RestSchema

  2. Declare @RequestMapping explicitly.

If the business code is not a new development but based on the development of Spring MVC, now the java chassis is based on the transformation, but also need to pay attention to the disabling of the DispatcherServlet, and its related features will no longer take effect.

In the following sections, about web JAVA application mode and web development mode, we'll introduce some details of the differences in Spring MVC mode.