编辑:现在更具体,我注意到我想在下面使用两个依赖项的冲突:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.3.1</version>
</dependency>
基本上,我试图忽略一个属性(@JsonIgnore),但是我的Jackson注释都没有起作用。甚至@JsonProperty。我试图在getters和setters方法中添加@JsonIgnore,但是行为相同。
我还尝试遵循官方文档,并尝试使用其他库
我来自控制器的回复不应显示已撤消。属性,但我得到了这个回应:
实际反应
{
"accessToken": "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJqb2huLmRvZUBleGFtcGxlLmNvbSIsImlzcyI6ImpvaG4uZG9lQGV4YW1wbGUuY29tIiwiaWF0IjoxNTI1MzI1Nzk1LCJleHAiOjE1MjUzMzI5OTV9.uri3pRwXQHHG09F-wM40qfuRMRVu_WBK3HlfquGvwYc",
"expiresAt": "2018-05-03T07:36:35.087Z[UTC]",
"expiresIn": 7199,
"issuedAt": "2018-05-03T05:36:35.087Z[UTC]",
"refreshToken": "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJqb2huLmRvZUBleGFtcGxlLmNvbSIsImlzcyI6ImpvaG4uZG9lQGV4YW1wbGUuY29tIiwiaWF0IjoxNTI1MzI1Nzk1LCJleHAiOjE1MjU5MzA1OTV9.xj2oytAVwiAIR8U2upJkPH_BdORuJUNbiicvuvGFz0w",
"revoked": false,
"type": "Bearer"
}
预期回应
{
"accessToken": "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJqb2huLmRvZUBleGFtcGxlLmNvbSIsImlzcyI6ImpvaG4uZG9lQGV4YW1wbGUuY29tIiwiaWF0IjoxNTI1MzI1Nzk1LCJleHAiOjE1MjUzMzI5OTV9.uri3pRwXQHHG09F-wM40qfuRMRVu_WBK3HlfquGvwYc",
"expiresAt": "2018-05-03T07:36:35.087Z[UTC]",
"expiresIn": 7199,
"issuedAt": "2018-05-03T05:36:35.087Z[UTC]",
"refreshToken": "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJqb2huLmRvZUBleGFtcGxlLmNvbSIsImlzcyI6ImpvaG4uZG9lQGV4YW1wbGUuY29tIiwiaWF0IjoxNTI1MzI1Nzk1LCJleHAiOjE1MjU5MzA1OTV9.xj2oytAVwiAIR8U2upJkPH_BdORuJUNbiicvuvGFz0w",
"type": "Bearer"
}
pom.xml(使用Maven)
<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.wedhany.fimper</groupId>
<artifactId>fimper</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>fimper</name>
<build>
<finalName>fimper</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<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>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>de.mkammerer</groupId>
<artifactId>argon2-jvm</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-dbcp</artifactId>
<version>9.0.1</version>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>5.0.7</version>
</dependency>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring4</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-jdk-http</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.17.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.9.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springframework.version}</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>Development</id>
<dependencies>
<dependency>
<groupId>com.github.blocoio</groupId>
<artifactId>faker</artifactId>
<version>1.2.7</version>
</dependency>
</dependencies>
</profile>
</profiles>
<properties>
<jersey.version>2.27</jersey.version>
<springframework.version>4.3.16.RELEASE</springframework.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Token.java(我的模型)
package com.wedhany.models;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.wedhany.models.enums.token.GrantType;
import com.wedhany.models.enums.token.Type;
import java.util.Date;
public class Token {
/**
* Attributes
*/
private String accessToken;
private String refreshToken;
@JsonIgnore
private boolean revoked;
@JsonProperty("expires_at")
private Date expiresAt;
private Date issuedAt;
private GrantType grantType;
private Type type;
private User user;
/**
* @return Token TTL in seconds.
*/
public long getExpiresIn() {
return this.expiresAt.getTime() < new Date().getTime()
? 0
: (this.expiresAt.getTime() - new Date().getTime()) / 1000;
}
/**
* @return Token that will grant authentication and authorization.
*/
public String getAccessToken() {
return accessToken;
}
/**
* @param accessToken Token string.
*/
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
/**
* @return Token used to request a new token.
*/
public String getRefreshToken() {
return refreshToken;
}
/**
* @return Invalid token if true.
*/
public boolean isRevoked() {
return revoked;
}
/**
* @param revoked True for invalid.
*/
public void setRevoked(boolean revoked) {
this.revoked = revoked;
}
/**
* @param refreshToken Refresh token.
*/
public void setRefreshToken(String refreshToken) {
this.refreshToken = refreshToken;
}
/**
* @return Token's expiration date.
*/
public Date getExpiresAt() {
return expiresAt;
}
/**
* @param expiresAt Token's expiration date.
*/
public void setExpiresAt(Date expiresAt) {
this.expiresAt = expiresAt;
}
/**
* @return Date where the token was requested.
*/
public Date getIssuedAt() {
return issuedAt;
}
/**
* @param issuedAt Date where the token was requested.
*/
public void setIssuedAt(Date issuedAt) {
this.issuedAt = issuedAt;
}
/**
* @return Type of the token.
*/
public Type getType() {
return type;
}
/**
* @param type Type of the token.
*/
public void setType(Type type) {
this.type = type;
}
/**
* @return How the token was claimed.
*/
public GrantType getGrantType() {
return grantType;
}
/**
* @param grantType Set token type of grant.
*/
public void setGrantType(GrantType grantType) {
this.grantType = grantType;
}
/**
* @return Owner of the token
*/
public User getUser() {
return user;
}
/**
* @param user Token's owner.
*/
public void setUser(User user) {
this.user = user;
}
}
身份验证控制器
package com.wedhany.controllers;
import com.wedhany.exceptions.AuthorizationException;
import com.wedhany.models.Token;
import com.wedhany.models.User;
import com.wedhany.services.AuthenticationService;
import org.springframework.beans.factory.annotation.Autowired;
import javax.security.sasl.AuthenticationException;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("auth")
public class AuthenticationController {
@Autowired
private AuthenticationService authenticationService;
@POST
@Path("login")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response login(User user, @HeaderParam("user-agent") String userAgent) throws Exception {
try {
// Authenticate the user using the credentials provided
this.authenticationService.authenticate(user.getEmail(), user.getPassword());
// Issue a token for the user
Token token = this.authenticationService.issueToken(user.getEmail(), userAgent);
// Return the token on the response
return Response.ok(token).build();
} catch (AuthorizationException e) {
return Response.status(Response.Status.UNAUTHORIZED).build();
} catch (AuthenticationException e) {
return Response.status(Response.Status.FORBIDDEN).build();
}
}
@POST
@Path("refresh")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response refresh(Token token, @HeaderParam("user-agent") String userAgent) throws AuthenticationException {
return Response.status(Response.Status.CREATED)
.entity(this.authenticationService.refresh(token.getRefreshToken(), userAgent))
.build();
}
@POST
@Path("register")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response register(User user) {
user = authenticationService.save(user);
return Response.status(Response.Status.CREATED)
.entity(user)
.build();
}
}
选择以下任一选项,但不能两者都选:
<!-- JSON-B (JSR-347) support -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
<version>2.27</version>
</dependency>
<!-- Jackson 2.x support -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.27</version>
</dependency>
Jackson和JSON-B都提供Java绑定中的JSON:
Jackson是一个非常成熟的JSON处理库。它非常灵活,并且具有大量的扩展模块。
JSON-B也称为JSR-347。这是JSON绑定的规范。实际的实现将由Eclipse Yasson提供,这是JSR-347的参考实现。
如果您愿意jersey-media-json-jackson
,则应该使用Jackson
注释。例如,要忽略属性,请使用@JsonIgnore
。
如果您想jersey-media-json-binding
使用,则应该使用JSON-B 批注。例如,要忽略属性,请使用@JsonbTransient
。
您正在使用jersey-bom
,一个 依赖项管理 工件,用于合并和集中管理依赖项版本(实际上没有将依赖项添加到项目中)。
因此,您无需指定org.glassfish.jersey
工件的版本。使用以下选项之一(不带version
):
<!-- JSON-B (JSR-347) support -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
</dependency>
<!-- Jackson 2.x support -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
</dependency>
问题内容: 但是当应用程序尝试在客户端请求之后调用它时,自动装配的Bean为空。在applicationContext.xml中,我只有 组件扫描 设置。 -- 我究竟做错了什么? UPD: 这是我的pom.xml https://bitbucket.org/spukhov/memo- ws/src/00724e00e3aa786f62fd0e43fe0606de6ae569df/pom.xml?
问题内容: 我正在尝试使用Jersey2构建示例REST服务: 这是我的POM依赖项: 这是我的web.xml: 这是我的ResourceConfig实现: 最后是我的示例类: 在将其部署到Tomcat时,我尝试访问以下URL: 这是我得到的例外: 无法理解我所缺少的。请问有人可以指出吗? 问题答案: 所有Jersey依赖项都应使用相同的版本,即应使用而不是。
我正在使用dropwizard和jersey2 rest客户端,我得到 然而,当我打开日志记录时,我看到 我怎样才能反序列化这个响应,而不是只得到400? 我的代码看起来像 亚洲账户基本上是空的 我添加了apiVerson,时间戳和错误到亚洲帐户,但它仍然不工作。任何帮助都很感激。
我正在开发运动衫2 RESTful服务 球衣2 v2。22.2 Spring3 v3。2.16 Jax rs v2。0.1 如果我收到@QueryParam template=“summary”,我需要排除一些GameDTO响应字段。 例如: 获取/Rest/v1/游戏?city=1必须返回: 然而 获取/Rest/v1/游戏?城市=1?模板=必须返回摘要: 如何排除字段?
我在jaxrs项目中有一个异常映射器来捕获所有NotFoundExcture并格式化响应。在Tomcat中运行时,在引发异常时调用异常映射器。我现在不得不转移到JBoss,所有的工作都很好,除了异常映射器不再被调用。 网状物xml 异常映射器
springboot 2和spring 5附带的默认jersey服务器和其他相关jersey依赖项jar是jersey-server-2.6,但是,当我试图将swagger-jersey2-jaxrs依赖项添加到我的maven项目中时,jersey服务器依赖项被覆盖为jersey-server-2.25.1。因此,我的应用程序无法启动,出现以下错误。 如果删除依赖项,则应用程序运行正常。 附加依赖
我刚接触Jersey并尝试实现一个可以分别接受application/json和multipart/form-data content-type的POSTendpoint。 下面是Spring Boot依赖关系: 我通过扩展ResourceConfig和注册我的REST API类配置了必要的资源,还包括一些标准的资源,如下所示: Maven依赖项中已经添加了“jersey-media-json-j
问题内容: 我有一个课程,如何让@JsonIgnore工作。即使将注释放在此处,它也不会影响输出。我正在使用杰克逊。 这是我的Controller方法: 这是我的servlet-context.xml: 问题答案: 我终于找到了解决方案。我将导入声明从 至 基本上,您必须确保在所有地方都使用相同的类。