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.
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:
Replace RestController with RestSchema
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.