相关疑难解决方法(0)

JAX-RS和java.time.LocalDate作为输入参数

使用JAX-RS和java.time.LocalDate(java8)的问题.

我想使用JSON将这样的对象传递给JAX-RS方法:

Person {
  java.time.LocalDate birthDay;
}
Run Code Online (Sandbox Code Playgroud)

我得到的例外是:

com.fasterxml.jackson.databind.JsonMappingException:找不到类型[simple type,class java.time.LocalDate]的合适构造函数:无法在[来源:io.undertow.servlet.spec.ServletInputStreamImpl@21cca2c1; 来自JSON对象(需要添加/启用类型信息?)实例化)line:2,column:3]

我怎样才能创建一种将json-dates映射到的拦截器java.time.LocalDate?我试过实现了一个MessageBodyReader,但如果LocalDate是另一个类中的一个字段,我必须MessageBodyReader为每个持有a的类写一个LocalDate(据我所知).

(Java EE7(仅使用javaee-api,不需要任何第三方依赖),JAX-RS,Java 8,Wildfly 8.2)

有什么建议?

java json jax-rs jackson java-8

12
推荐指数
1
解决办法
2万
查看次数

如何让JAX-RS将Java 8 LocalDateTime属性作为JavaScript样式的日期字符串返回?

我使用JAX-RS方法注释创建了一个RESTful Web服务:

@GET
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public MyThing test()
{
    MyThing myObject = new MyThing(LocalDateTime.now());
    return myObject;
}
Run Code Online (Sandbox Code Playgroud)

这很好用,但我想调整一件事:如果返回的Java对象包含新Java 8 LocalDateTime类型的属性,则表示为JSON对象:

{"myDateTimeProperty":{"hour":14,"minute":32,"second":39,"year":2014,"month":"NOVEMBER","dayOfMonth":6,"dayOfWeek":"THURSDAY","dayOfYear":310,"monthValue":11,"nano":0,"chronology":{"calendarType":"iso8601","id":"ISO"}},...}
Run Code Online (Sandbox Code Playgroud)

如何告诉JAX-RS返回一个JavaScript Date.toJSON() - 样式字符串

{"myDateTimeProperty":"2014-11-07T15:06:36.545Z",...}
Run Code Online (Sandbox Code Playgroud)

代替?

java json jax-rs java-8 java-time

9
推荐指数
2
解决办法
9250
查看次数

泽西解析Java 8日期时间

这是我的用户类,我在我的数据库中保存了ISO兼容的日期时间.

public class User  {

    @Id
    private String id;

    private String email;

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    private LocalDateTime loginDate;

 }
Run Code Online (Sandbox Code Playgroud)

这是我的泽西控制器:

@POST
@Consumes("application/json")
@Produces("application/json")

public Response create(  User user) {

    Map<Object, Object> apiResponse = new HashMap<Object, Object>();
    Map<Object, Object> response  = new HashMap<Object, Object>();


    user = (User) userService.create(user);

}
Run Code Online (Sandbox Code Playgroud)

我怎样才能在运动衫中使用像这样的日期时间格式?是否可以自动发送String数据时间并创建Java 8日期时间对象?

{        
    "email" : "imz.mrz@gmail.com"
    "loginDate" : "2015-04-17T06:06:51.465Z"
} 
Run Code Online (Sandbox Code Playgroud) #

更新:

我使用的是Spring boot jersey,还有其他jsr包

  <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-jersey</artifactId>
 </dependency>
Run Code Online (Sandbox Code Playgroud)

所以我删除了除spring-boot-jersey包之外的所有包.对LocalDateTime使用此注释

  @JsonDeserialize(using =  LocalDateTimeDeserializer.class)
Run Code Online (Sandbox Code Playgroud)

这样我可以使用ISODate并将ISODate()保存到mongodb并生成完整格式化的mongodb LocalDateTime到前端.

问题解决了.

java datetime jax-rs jersey java-8

8
推荐指数
1
解决办法
1万
查看次数

有效的方法让杰克逊将Java 8 Instant序列化为纪元毫秒?

使用带有Jackson JSON解析后端的Spring RestControllers,前端使用AngularJS.我正在寻找一种有效的方法让Jackson序列化一个Instant作为纪元毫秒,以便随后使用JavaScript代码.(在浏览器方面,我希望通过Angular的日期过滤器提供epoch ms :{{myInstantVal | date:'short' }}为我所需的日期格式.)

在Java方面,杰克逊将使用的吸气剂就是:

public Instant getMyInstantVal() { return myInstantVal; }
Run Code Online (Sandbox Code Playgroud)

序列化不会按原样运行,因为对于Instant ,jackson-datatype-jsr310默认不返回Epoch毫秒.我看着将@JsonFormat添加到上面的getter中以将Instant变形为前端可以使用的东西,但它遇到两个问题:(1)我可以提供它的模式显然仅限于SimpleDateFormat,它不提供"epoch milliseconds"选项,以及(2)当我尝试将Instant作为格式化日期发送到浏览器时,Jackson会抛出异常,因为@JsonFormat注释需要Instants的TimeZone属性,我不想硬编码因为它会因用户而异.

到目前为止我的解决方案(并且工作正常)是使用@JsonGetter创建替换的getter ,这会导致Jackson使用此方法来序列化myInstantVal:

@JsonGetter("myInstantVal")
public long getMyInstantValEpoch() {
    return myInstantVal.toEpochMilli();
}
Run Code Online (Sandbox Code Playgroud)

这是正确的方法吗?或者是否有一个很好的注释,我错过了我可以放在getMyInstantVal()所以我不必创建这些额外的方法?

spring json jackson angularjs java-time

8
推荐指数
2
解决办法
5350
查看次数

使用 Jersey 客户端,Java 8 Date API (Jsr310) 的问题

我正在使用 jersey 客户端进行一些 PoC 来使用 REST 服务,但我在使用 LocalDateTime 格式的字段时遇到了问题。REST 服务响应如下:

{
    "id": 12,
    "infoText": "Info 1234",
    "creationDateTime": "2001-12-12T13:40:30"
}
Run Code Online (Sandbox Code Playgroud)

和我的实体类:

package com.my.poc.jerseyclient;

import java.time.LocalDateTime;

public class Info {

    private Long id;
    private String infoText;
    private LocalDateTime creationDateTime;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getInfoText() {
        return infoText;
    }

    public void setInfoText(String infoText) {
        this.infoText = infoText;
    }

    public LocalDateTime getCreationDateTime() {
        return creationDateTime;
    }

    public void setCreationDateTime(LocalDateTime creationDateTime) …
Run Code Online (Sandbox Code Playgroud)

java rest jersey jackson jsr310

6
推荐指数
1
解决办法
4282
查看次数

使用ObjectMapper添加JAR使我的ObjectMapper不可发现

当jar中从依赖项中定义了另一个对象映射器时,如何使我的对象映射器工作?

我正在尝试使用Swagger和在Jetty下运行的Jersey 2.问题是,只要我将Swagger JAX-RX jar添加到类路径中,就不会发现我的对象映射器,因此我丢失了对象的自定义序列化.

这是我的对象映射器定义的方式

@Provider
public class ObjectMapperProvider implements ContextResolver<ObjectMapper> {
}
Run Code Online (Sandbox Code Playgroud)

我已经向Swagger的维护者发布了一个问题,你可以在那里阅读详细信息.

在Jersey的内部调试数小时后,我发现Swagger自己的对象映射器com.wordnik.swagger.jaxrs.json.JacksonJsonProvider调用super.setMapper(commonMapper)将非null值设置为ProviderBase._mapperConfig._mapper.稍后当http请求处理程序尝试序列化我的类调用的实例时,ProviderBase.locateMapper其中有以下正文

public MAPPER locateMapper(Class<?> type, MediaType mediaType)
{
    // First: were we configured with a specific instance?
    MAPPER m = _mapperConfig.getConfiguredMapper();
    if (m == null) {
        // If not, maybe we can get one configured via context?
        m = _locateMapperViaProvider(type, mediaType);
        if (m == null) {
            // If not, let's get …
Run Code Online (Sandbox Code Playgroud)

java jax-rs jersey swagger jersey-2.0

5
推荐指数
1
解决办法
664
查看次数

Jackson:将纪元反序列化为 LocalDate

我有以下 JSON:

{
      "id" : "1",
      "birthday" : 401280850089
}
Run Code Online (Sandbox Code Playgroud)

和 POJO 类:

public class FbProfile {
    long id;
    @JsonDeserialize(using = LocalDateDeserializer.class)
    LocalDate birthday;
}
Run Code Online (Sandbox Code Playgroud)

我正在使用 Jackson 进行反序列化:

public FbProfile loadFbProfile(File file) throws JsonParseException, JsonMappingException, IOException {
    ObjectMapper mapper = new ObjectMapper();
    FbProfile profile = mapper.readValue(file, FbProfile.class);
    return profile;
}
Run Code Online (Sandbox Code Playgroud)

但它抛出一个异常:

com.fasterxml.jackson.databind.JsonMappingException:意外的令牌(VALUE_NUMBER_INT),预期的 VALUE_STRING:预期的数组或字符串。

我怎样才能反序列化纪元LocalDate?我想补充一点,如果我将数据类型从更改LocalDatejava.util.Date它,它工作得很好。因此,也许最好反序列化java.util.Date并创建 getter 和 setter 来进行 to/from 的转换LocalDate

java json epoch jackson localdate

5
推荐指数
1
解决办法
6296
查看次数

无法从 String 值实例化 [简单类型,类 java.time.LocalDate] 类型的值

我有一个这样的课程:

@Data
@NoArgsConstructor(force = true)
@AllArgsConstructor(staticName = "of")
public class BusinessPeriodDTO {
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
    LocalDate startDate;
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
    LocalDate endDate;
}
Run Code Online (Sandbox Code Playgroud)

我在另一个类中使用了这个类,我们称之为 PurchaseOrder

@Entity
@Data
@NoArgsConstructor(access = AccessLevel.PROTECTED, force = true)
public class PurchaseOrder {
    @EmbeddedId
    PurchaseOrderID id;

    @Embedded
    BusinessPeriod rentalPeriod;

    public static PurchaseOrder of(PurchaseOrderID id, BusinessPeriod period) {
        PurchaseOrder po = new PurchaseOrder();
        po.id = id;

        po.rentalPeriod = period;

        return po;
    }
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用 jakson 和这个 JSON 填充 purchaseOrder 记录:

 {
     "_class": "com.rentit.sales.domain.model.PurchaseOrder",
     "id": 1,
     "rentalPeriod": {
         "startDate": …
Run Code Online (Sandbox Code Playgroud)

java spring

4
推荐指数
2
解决办法
2万
查看次数

无法将类型 [java.lang.String] 的属性值转换为所需类型 [java.time.LocalDate]

{
"toDepartureDate": "2016-12-28",
"fromDepartureDate": "2016-12-28"
}
Run Code Online (Sandbox Code Playgroud)

我想将上面的字符串日期以 json 格式发布java.time.LocalDate,但我收到 400 Bad Request 作为错误。有人可以帮忙吗?我已经使用过@JsonFormat,但它也没有帮助我。

@JsonFormat(shape=JsonFormat.Shape.STRING,pattern="yyyy-MM-dd",timezone = "GMT+5:30")

private LocalDate fromDepartureDate;

@JsonFormat(shape=JsonFormat.Shape.STRING,pattern="yyyy-MM-dd",timezone = "GMT+5:30")
private LocalDate toDepartureDate;

{
  "timestamp": 1482942147246,
  "status": 400,
  "error": "Bad Request",
  "exception": "org.springframework.validation.BindException",
  "errors": [
    {
      "codes": [
        "typeMismatch.flightReportSearchDto.fromDepartureDate",
        "typeMismatch.fromDepartureDate",
        "typeMismatch.java.time.LocalDate",
        "typeMismatch"
      ],
      "arguments": [
        {
          "codes": [
            "flightReportSearchDto.fromDepartureDate",
            "fromDepartureDate"
          ],
          "arguments": null,
          "defaultMessage": "fromDepartureDate",
          "code": "fromDepartureDate"
        }
      ],
      "defaultMessage": "Failed to convert property value of type [java.lang.String] to required type [java.time.LocalDate] for property …
Run Code Online (Sandbox Code Playgroud)

java json jackson spring-boot

3
推荐指数
1
解决办法
1万
查看次数

在 Spring Rest API 中配置 LocaldateTime

我使用 Java 10 和最新的 Spring spring-boot-starter-parent 2.1.0.RELEASE

POM 配置:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
    </parent>

    <dependencies>          
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.9.7</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.9.7</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-jaxb-annotations</artifactId>
            <version>2.9.7</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <version>2.9.7</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.woodstox</groupId>
            <artifactId>woodstox-core-asl</artifactId>
            <version>4.4.1</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
            <version>2.3.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-jdk8</artifactId>
            <version>1.2.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jxls</groupId>
            <artifactId>jxls-poi</artifactId>
            <version>1.0.15</version>
        </dependency> …
Run Code Online (Sandbox Code Playgroud)

spring spring-data spring-data-jpa spring-boot

3
推荐指数
2
解决办法
6668
查看次数