1. Interface design

We will implement two interfaces in the system:

GET http://localhost:8888/hello

GET http://localhost:8888/hello/way

The first interface “/hello” will return “Hello World!” String; The second interface, “/hello/way,” returns a JSON string containing user information.

Second, system configuration

We need to add the following dependencies to our application:

<dependency>
    <groupId>org.springframework</groupId>
	<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
    <groupId>org.eclipse.jetty</groupId>
	<artifactId>jetty-servlet</artifactId>
	<scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
	<artifactId>javax.servlet-api</artifactId>
	<scope>provided</scope>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
</dependency>
Copy the code

Among them,

Spring-webmvc is designed to use the functionality of Spring MVC

Jetty-servlet is designed to provide built-in servlet containers so that we can run our applications directly without relying on external containers.

Jackson-core and ‘jackson-databind’ provide JSON serialization for our application.

Third, background coding implementation

The domain model

Create a User class that represents User information.

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class User {

    private String username;
	
    private Integer age;
}
Copy the code

The controller

Create HelloController to handle user requests.

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(a) {
        return "Hello World!";
    }

    @RequestMapping("/hello/way")
    public User helloWay(a) {
        return new User("luopeng".30); }}Copy the code

Where a method mapped to “/hello” returns “Hello World!” String; Mapping to “/hello/way” returns a JSON string containing user information

Application configuration

In this application, we use Java annotation-based configuration.

AppConfiguration is our main application configuration:

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@ComponentScan(basePackages = { "com.epom" })  
@Import({ MvcConfiguration.class })
public class AppConfiguration {}Copy the code

AppConfiguration scans the files in the com.epom package and automatically registers related beans.

AppConfiguration also introduces MVC configuration class MvcConfiguration:

import java.util.List;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@EnableWebMvc
@Configuration
public class MvcConfiguration implements WebMvcConfigurer {

    public void extendMessageConverters(List
       
        > converters)
       > {
        converters.add(newMappingJackson2HttpMessageConverter()); }}Copy the code

The MvcConfiguration configuration class enables MVC functionality on the one hand and adds Jackson JSON converters on the other.

Finally, we need to introduce Jetty server:

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import com.epom.web.AppConfiguration;

public class JettyServer {

	private static final int DEFAULT_PORT = 8888;
	private static final String CONTEXT_PATH = "/";
	private static final String MAPPING_URL = "/ *";

	public void run(a) throws Exception {
		Server server = new Server(DEFAULT_PORT);
		server.setHandler(servletContextHandler(webApplicationContext()));
		server.start();
		server.join();
	}

	private ServletContextHandler servletContextHandler(WebApplicationContext context) {
		ServletContextHandler handler = new ServletContextHandler();
		handler.setContextPath(CONTEXT_PATH);
		handler.addServlet(new ServletHolder(new DispatcherServlet(context)), MAPPING_URL);
		handler.addEventListener(new ContextLoaderListener(context));
		return handler;
	}

	private WebApplicationContext webApplicationContext(a) {
		AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
		context.register(AppConfiguration.class);
		returncontext; }}Copy the code

JettyServer will start in the Application class:

public class Application {

	public static void main(String[] args) throws Exception {
        newJettyServer().run();; }}Copy the code

Four, run,

The 2019-12-18 11:18:35. 637: INFO: : the main: Logging the initialized @ 140 ms to org. Eclipse. Jetty. Util. The StdErrLog 11:18:35. 2019-12-18, 731: the INFO: oejs. Server: main: Jetty - 9.4.22. V20191022; Built: the 2019-10-22 T13: then. 455 z; git: b1e6b55512e008f7fbdf1cbea4ff8a6446d1073b; The JVM 1.8.0 comes with _191 b12-2019-12-18 11:18:35. 762: INFO: oejshC. ROOT: main: Initializing Spring Root WebApplicationContext December 18, 2019 11:18:35 morning org. Springframework. Web. Context. ContextLoader initWebApplicationContext information: Root WebApplicationContext: Initialization started December 18, 2019 11:18:36 morning org. Springframework. Web. Context. ContextLoader initWebApplicationContext information: Root WebApplicationContext initialized in 501 ms 11:18:36 2019-12-18. 263: INFO: oejshC. Root: main: The Initializing Spring DispatcherServlet 'org. Springframework. Web. Servlet. DispatcherServlet - 62043840' December 18, 2019 11:18:36 morning org. Springframework. Web. Servlet. FrameworkServlet initServletBean information: The Initializing the Servlet 'org. Springframework. Web. Servlet. DispatcherServlet - 62043840' December 18, 2019 11:18:36 morning org. Springframework. Web. Servlet. FrameworkServlet initServletBean information: Completed initialization in 5 ms 11:18:36 2019-12-18. 283: INFO: oejsh. ContextHandler: main: Started o.e.j.s.ServletContextHandler@2631f68c{/,null,AVAILABLE} 2019-12-18 11:18:36. 383: INFO: oejs. AbstractConnector: main: Started ServerConnector @ 368239 c8 {HTTP / 1.1, [HTTP / 1.1]} {8888} 0.0.0.0: the 2019-12-18 11:18:36. 383: INFO: oejs. Server: main: Started @888msCopy the code

Respectively in the browser to access the “http://localhost:8888/hello” and “http://localhost:8888/hello/way” address for testing, can see the response of the figure 1 and figure 2.