JerseyConfig 类
package com.xgt.config.jersey;
import javax.ws.rs.ApplicationPath;
import com.xgt.controller.sync.*;
import org.glassfish.jersey.server.ResourceConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @时间 2018年2月4日15:30:16
* @author Mr.Li
*
*/
@ApplicationPath("/restful") 顶级uri 即基本路径
public class JerseyConfig extends ResourceConfig {
private static final Logger logger = LoggerFactory.getLogger(JerseyConfig.class);
public JerseyConfig() {
logger.info("......JerseyConfig.....start...");
// 构造函数,在这里注册需要使用的内容,(过滤器,拦截器,API等)
// 注册类的方式
// register(Demo.class);
// register(RequestContextFilter.class);
register(ImgTextRemoteServerController.class);
register(LabourerRemoteServerController.class);
register(RemoteServerQuestionRelController.class);
register(RemoteServerTrainExamController.class);
register(RemoteServerVideoController.class);
// 注册包的方式 , 打成jar包运行,会报错 ResourceFinderException :系统找不到路径
// packages("com.xgt.controller.sync");
}
}
JerseySpringConfig 注入到spring中的配置
package com.xgt.config.jersey;
import com.xgt.config.SimpleCORSFilter;
import com.xgt.filter.CharacterEncodingFilter;
import org.glassfish.jersey.servlet.ServletContainer;
import org.glassfish.jersey.servlet.ServletProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* copyright © 2008-2016 CTIM. All Right Reserved.
* Created by CC on 2016/9/9.
* Desc:
*/
@Configuration
public class JerseySpringConfig {
private static final Logger logger = LoggerFactory.getLogger(JerseySpringConfig.class);
@Bean
public ServletRegistrationBean jersetServlet() {
logger.info("......jersetServlet..start.....");
ServletRegistrationBean registration = new ServletRegistrationBean(
// 顶级uri 用来区分SpringMVC
new ServletContainer(), "/restful/*");
registration.addInitParameter(
ServletProperties.JAXRS_APPLICATION_CLASS,
JerseyConfig.class.getName());
return registration;
}
}
测试controller
package com.xgt.config.jersey;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.xgt.dao.BuildLabourerDao;
import com.xgt.service.fingerprint.FingerPrintService;
import com.xgt.service.rdcard.RdCardService;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import java.util.HashMap;
import java.util.Map;
@Controller
@Path("/jersey")
public class JerseyController {
// path注解指定路径,get注解指定访问方式,produces注解指定了返回值类型,这里返回JSON
@Path("/city")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Map<String,Object> get() {
Map<String,Object> map = new HashMap<>();
map.put("aaa", "aaaaa");
return map;
}
}
依赖的pom
<!-- restful webService start -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-client -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19.4</version>
</dependency>
<!-- restful webService end -->