Build spring web mvc REST style HelloWorld in 5 minutes

Posted by rheroux on Thu, 13 Jun 2019 19:36:38 +0200

Of course, the goal of this article is not to be speed, but to show that it is now easy to build a Spring web mvc Rest-style HelloWorld application.However, if you've seen the Spring Boot project, it may take up to three minutes to build a simple Rest-style application.Go back and share.


My Building Environment


Maven 3

Servlet3 Container


Create Project

First use Maven to create a normal Maven application, not a web application.


Add Dependency

<!-- servlet 3 -->

        <!--spring context -->

        <!--spring webmvc -->

        <!--jackson -->

servlet3 dependency scope is provided to represent environment provisioning, then adds spring-context-support and spring-webmvc dependencies, and finally jackson dependencies for json.It's very simple and clear.


Add a maven plugin

For testing purposes, add a jetty maven plug-in so that it runs directly using mvn jetty:run.

                        <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">



package com.sishuok.entity;


 * <p>User: Zhang Kaitao
 * <p>Date: 13-12-19
 * <p>Version: 1.0
public class User implements Serializable {
    private Long id;
    private String name;

    public Long getId() {
        return id;

    public void setId(Long id) { = id;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        User user = (User) o;

        if (id != null ? !id.equals( : != null) return false;

        return true;

    public int hashCode() {
        return id != null ? id.hashCode() : 0;



package com.sishuok.controller;

import com.sishuok.entity.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

 * <p>User: Zhang Kaitao
 * <p>Date: 13-12-19
 * <p>Version: 1.0
public class UserController {

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public User view(@PathVariable("id") Long id) {
        User user = new User();
        return user;

Specifically, do not introduce more, please refer to: New Spring4 Feature - Enhancement of Web Development "And" Learn Spring MVC from me>.



SpringMVC Annotation Style Configuration

public class AppConfig {

Refer to the specific meaning of " New Spring4 Feature - Groovy Bean Definition DSL "Section.


Servlet3 Container Start Initializer

Programmatically register Servlet at Servlet container startup

package com.sishuok;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

 * <p>User: Zhang Kaitao
 * <p>Date: 13-12-19
 * <p>Version: 1.0
public class AppInitializer implements WebApplicationInitializer {

    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext webApplicationContext =
                new AnnotationConfigWebApplicationContext();

        DispatcherServlet dispatcherServlet = new DispatcherServlet(webApplicationContext);
        ServletRegistration.Dynamic dynamic = servletContext.addServlet("dispatcherServlet", dispatcherServlet);


Refer to the specific meaning of " Learn Spring3 from me Of Chapter 12 And New Spring4 Feature - Groovy Bean Definition DSL And My github servlet3-showcase "Section.(


Then run mvn jetty:run and your browser will enter the following address to get our json data.



Reference example github address: springmvc-rest-helloworld


A very simple Rest-style web application is set up and ready to go.


The next article introduces building a spring mvc REST-style HelloWorld using Spring Boot in 2 minutes.



Reprinted at:

Topics: Spring Maven Jetty REST