• eclipse + maven + org.glassfish.jersey 创建 webapi


    org.glassfish.jersey 和 com.sun.jersey 的区别是,jersy version 2 之前是 com.sun.jersy, 之后改名为 org.glassfish.jersey, 所以想用 jersey 新版本就用 org.glassfish.jersey;

    创建

    支持返回 json 对象

    pom

    
    	<dependency>
    		<groupId>org.glassfish.jersey.media</groupId>
    		<artifactId>jersey-media-json-jackson</artifactId>
    		<version>2.25.1</version>
    	</dependency>
    
    
        @GET 
        @Produces(MediaType.APPLICATION_JSON)
        public UserInfo getIt() {
            UserInfo user =  new UserInfo();
            user.setId("1");
            user.setName("grissom");
            return user;
        }
    
    
    错误: Exception in thread "main" java.lang.NoSuchMethodError: org.glassfish.jersey.CommonProperties.getValue(Ljava/util/Map;Ljavax/ws/rs/RuntimeType;Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Class;)Ljava/lang/Object; ,修改jersey 版本号,和 json 一致
    	<properties>
    		<jersey.version>2.25.1</jersey.version>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    	</properties>
    
    
    错误: org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo

    去掉或注释下面这段

    		<!-- uncomment this to get JSON support: 
    		<dependency> 
    		    <groupId>org.glassfish.jersey.media</groupId> 
    		    <artifactId>jersey-media-moxy</artifactId> 
    		</dependency>-->
    
    
    错误 Caused by: java.lang.ClassNotFoundException: org.glassfish.jersey.client.ClientConfig ,添加
    		<dependency>
    			<groupId>org.glassfish.jersey.core</groupId>
    			<artifactId>jersey-client</artifactId>
    			<version>2.25.1</version>
    			<scope>provided</scope>
    		</dependency>
    
    错误 Exception in thread "main" java.lang.NoClassDefFoundError: org/objectweb/asm/ClassVisitor ,添加
     	<dependency>
    			<groupId>asm</groupId>
    			<artifactId>asm</artifactId>
    			<version>3.3.1</version>
    		</dependency>
    

    我的 demo pom:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    
    	<modelVersion>4.0.0</modelVersion>
    
    	<groupId>com.easymerylife.member</groupId>
    	<artifactId>member</artifactId>
    	<packaging>jar</packaging>
    	<version>0.0.1-SNAPSHOT</version>
    	<name>member</name>
    
    	<dependencyManagement>
    		<dependencies>
    			<dependency>
    				<groupId>org.glassfish.jersey</groupId>
    				<artifactId>jersey-bom</artifactId>
    				<version>${jersey.version}</version>
    				<type>pom</type>
    				<scope>import</scope>
    			</dependency>
    		</dependencies>
    	</dependencyManagement>
    
    	<dependencies>
    		<dependency>
    			<groupId>org.glassfish.jersey.containers</groupId>
    			<artifactId>jersey-container-grizzly2-http</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.glassfish.jersey.media</groupId>
    			<artifactId>jersey-media-json-jackson</artifactId>
    			<version>2.25.1</version>
    		</dependency>
    		<dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    			<version>4.9</version>
    			<scope>test</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.glassfish.jersey.core</groupId>
    			<artifactId>jersey-client</artifactId>
    			<version>2.25.1</version>
    			<scope>provided</scope>
    		</dependency>
    		<dependency>
    			<groupId>asm</groupId>
    			<artifactId>asm</artifactId>
    			<version>3.3.1</version>
    		</dependency>
    	</dependencies>
    
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.apache.maven.plugins</groupId>
    				<artifactId>maven-compiler-plugin</artifactId>
    				<version>2.5.1</version>
    				<inherited>true</inherited>
    				<configuration>
    					<source>1.6</source>
    					<target>1.6</target>
    				</configuration>
    			</plugin>
    			<plugin>
    				<groupId>org.codehaus.mojo</groupId>
    				<artifactId>exec-maven-plugin</artifactId>
    				<version>1.2.1</version>
    				<executions>
    					<execution>
    						<goals>
    							<goal>java</goal>
    						</goals>
    					</execution>
    				</executions>
    				<configuration>
    					<mainClass>com.easymerylife.member.member.Main</mainClass>
    				</configuration>
    			</plugin>
    		</plugins>
    	</build>
    
    	<properties>
    		<jersey.version>2.25.1</jersey.version>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    	</properties>
    </project>
    
    
    
  • 相关阅读:
    LED显示屏设备智能监控运维管理解决方案
    网络监控系统七大开源工具分析
    银行设备综合监控运维管理解决方案
    柯南「云断案」不再难,身在何处都如亲临现场
    七牛云联合云上钢琴,推动智慧教育生态繁荣
    七牛云联手开泰银行,加速等保 2.0 合规落地
    【七牛云X创客匠人】知识付费私域流量场中的技术实践
    七牛云正式加入 CNCF,积极推动云原生全球发展
    Protocol buffer 编码和解码 谷歌 整数变长编码
    mybatis利用动态SQL进行模糊查询遇到的问题
  • 原文地址:https://www.cnblogs.com/grissom007/p/6993015.html
Copyright © 2020-2023  润新知