Jersey integrates Spring Boot

Introduction to jersey

       JAX-RS is a new technology introduced in JAVA EE6. JAX-RS, the Java API for RESTful Web Services, is an application programming interface in the Java programming language that supports the creation of Web services according to the Representational State Transfer (REST) ​​architectural style. JAX-RS uses the Java annotations introduced in Java SE5 to simplify the development and deployment of web service clients and servers. 

 

Introduction to RESTful Web Services

    REST was introduced in 2000 by Roy Fielding, one of the lead authors of versions 1.0 and 1.1 of the HTTP specification, in his doctoral dissertation.

    The most important concept in REST is resources, identified using global IDs (usually URIs). Client applications use HTTP methods (GET/POST/PUT/DELETE) to manipulate resources or sets of resources. RESTful web services are web services implemented using HTTP and REST principles. In general, a RESTful web service should define the following aspects:

 

  • The base/root URI of the web service, such as http://host/<appcontext>/resources.
  • Supports response data of MIME type, including JSON/XML/ATOM, etc.
  • A collection of operations supported by the service (such as POST, GET, PUT, or DELETE)

Let's start integrating

1. Create a maven project

Create process by yourself

2. pom.xml configuration:

 

<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
	</properties>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.2.RELEASE</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jersey</artifactId>
		</dependency>
	</dependencies>

 

 

3. Create the JerseyConfig class:

 

import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.web.filter.RequestContextFilter;

public class JerseyConfig extends ResourceConfig {
    public JerseyConfig() {
       register(RequestContextFilter.class);
       //Configure restful package.
       packages("com.services");
    }
}

 4. Create the RestResource class:

 

This class is accessible through rest requests

 

package com.services;

import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.springframework.stereotype.Component;

@Path("/")
@Component
public class RestResource {
	
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/hello")
	public Map<String, Object> hello() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", "1");
		map.put("codeMsg", "success");
		return map;
	}
	
}

 5. Create the app startup class

 

import org.glassfish.jersey.servlet.ServletContainer;
import org.glassfish.jersey.servlet.ServletProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class App {
   
    @Bean
    public ServletRegistrationBean jerseyServlet() {
      ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/rest/*");
       // our rest resources will be available in the path /rest/*
       registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyConfig.class.getName());
      
       return registration;	
    }
   
    public static void main(String[] args) {
       SpringApplication.run(App.class, args);
       
    }
}

 

 6. Right-click the app to run

http://127.0.0.1:8080/rest/hello

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327002435&siteId=291194637