小编dve*_*opp的帖子

在测试中使用@MockBean强制重新加载Application Context

我在Spring Framework上运行了几个集成测试,扩展了名为BaseITCase的基类.
像这样:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {AppCacheConfiguration.class, TestConfiguration.class}, loader = SpringBootContextLoader.class)
@Transactional
@WebMvcTest
public abstract class BaseITCase{...}
...
public class UserControllerTest extends BaseITCase {...}
Run Code Online (Sandbox Code Playgroud)

问题是其中一个测试有几个声明:@MockBean在它内部以及执行此测试的那一刻,Spring重新创建上下文,并且在此之后的测试有时使用错误的bean(来自为@的测试创建的上下文) MockBean).我通过检查bean有不同的哈希码来发现这一点.

当我使用@EventListener时,它变得非常关键.因为调用了错误上下文(已经完成执行的测试类的上下文)的侦听器,并且我在那里有错误的bean.

那有什么解决方法吗?

我试图将所有@MockBean声明移动到基本类,它工作正常,因为没有创建新的上下文.但是,它使基础课太重了.此外,我尝试为此测试创建一个脏上下文,但接下来的测试失败,并显示上下文已被关闭的消息.

java spring spring-test mockito springmockito

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

在 flyway 中删除迁移

我遇到以下问题:

org.flywaydb.core.api.FlywayException: Validate failed: Detected applied migration not resolved locally: 1.44
Run Code Online (Sandbox Code Playgroud)

当我了解到我在 1.44 中添加的数据无效并且我不想在旧环境中处理它,但在新环境中我不想要这些数据时,发生了这种情况。我想要我将在新迁移中插入的数据(例如 1.48)。

  • 如果我更改旧迁移,由于校验和,它会在旧环境中失败;
  • 如果我离开它,在新环境中我将拥有 1.44 中的无效数据;

如何删除它以便我完成我需要的操作而不是出错?正确的方法是什么?

flyway

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

通过Zuul上传大文件

我在通过zuul上传大文件时遇到了问题.我正在使用apache-commons文件上传(https://commons.apache.org/proper/commons-fileupload/)来传输大文件以及我在前面使用zuul.在我的Spring Boot应用程序中,我禁用了Spring提供的上传来使用apache commons中的一个:

  spring:
      http:
          multipart:
              enabled: false
Run Code Online (Sandbox Code Playgroud)

控制器看起来像这样:

public ResponseEntity insertFile(@PathVariable Long profileId, 
    HttpServletRequest request) throws Exception {
    ServletFileUpload upload = new ServletFileUpload();
    FileItemIterator uploadItemIterator = upload.getItemIterator(request);
    if (!uploadItemIterator.hasNext()) {
        throw new FileUploadException("FileItemIterator was empty"); 
    } 
    while (uploadItemIterator.hasNext()) {
        FileItemStream fileItemStream = uploadItemIterator.next();
        if (fileItemStream.isFormField()) {
            continue; 
        } 
       //do stuff 
    } 
    return new ResponseEntity(HttpStatus.OK);
} 
Run Code Online (Sandbox Code Playgroud)

如果我直接访问我的应用程序(没有zuul),文件上传按预期工作.但是,如果通过zuul访问它,则FileItemIterator没有要遍历的项目,并且请求立即生成错误(ERR_CONNECTION_RESET).对于zuul,我也禁用了Spring给出的多部分.否则,它的工作原理.但是,文件未流式传输.只有在我进入控制器(常规的Spring行为)之后才会完全加载它们.有没有办法在zuul中使用apache-commons流选项?

java spring apache-commons-fileupload spring-boot netflix-zuul

7
推荐指数
1
解决办法
5821
查看次数

按开始日期对事件排序

我需要能够使用Microsoft Graph API在ASC/DESC的两个方向上获取事件.我正在尝试使用以下API来实现这一目标:

https://graph.microsoft.com/v1.0/me/events?$orderby=start
Run Code Online (Sandbox Code Playgroud)

但是,当我执行请求时,我收到以下错误:

{
    "error": {
        "code": "BadRequest",
        "message": "The $orderby expression must evaluate to a single value of primitive type.",
        "innerError": {
            "request-id": "c00d676d-ef8e-418b-8561-80e08729da71",
            "date": "2017-11-16T13:31:59"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

另外,我试图直接访问日期:

https://graph.microsoft.com/v1.0/me/events?$orderby=start.dateTime
Run Code Online (Sandbox Code Playgroud)

得到以下错误:

{
    "error": {
        "code": "BadRequest",
        "message": "The child type 'start.dateTime' in a cast was not an entity type. Casts can only be performed on entity types.",
        "innerError": {
            "request-id": "240342f5-d7f6-430b-9bd0-190dc3e1f73b",
            "date": "2017-11-16T13:32:39"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法按ASC/DESC顺序按日期对事件进行排序?

outlook-restapi microsoft-graph

7
推荐指数
1
解决办法
1420
查看次数

在观看Google日历推送通知时收到错误"WEB_HOOK频道不可用"

目前我开始得到:

{
  "code" : 400,
  "errors" : [ {
    "domain" : "push",
    "message" : "WEB_HOOK channel unavailable for: {address=https://...}",
    "reason" : "channelUnknown"
  } ],
  "message" : "WEB_HOOK channel unavailable for: {address=https://...}"
}
Run Code Online (Sandbox Code Playgroud)

当我尝试开始观看Google推送通知时.我已经验证了我的域名,并且该URL可用于Internet中的帖子请求.通知只是没有实现.这可能有什么问题?

google-calendar-api google-api push-notification google-oauth

7
推荐指数
1
解决办法
308
查看次数

Java没有关于所有IANA时区的信息

我正在尝试将来自前端的值映射到ZoneId类,如下所示:

Optional.ofNullable(timeZone).map(ZoneId::of).orElse(null)
Run Code Online (Sandbox Code Playgroud)

对于大多数时区,它工作正常,但是,对于某些值,Java抛出异常:

java.time.zone.ZoneRulesException: Unknown time-zone ID: America/Punta_Arenas
Run Code Online (Sandbox Code Playgroud)

但是,根据IANA,它是一个有效的时区:https: //www.iana.org/time-zones

Zone America/Punta_Arenas -4:43:40 - LMT 1890

我在考虑为这些时区使用偏移量(只是硬编码值),但我想应该有更方便的方法来解决这个问题.有没有办法可以处理Java?

其他不受支持的时区:

  • 美国/ Punta_Arenas
  • 亚洲/阿特劳
  • 亚洲/法马古斯塔
  • 亚洲/仰光
  • 美东时间
  • 欧洲/萨拉托夫
  • HST
  • MST

我的Java版本:"1.8.0_121"Java(TM)SE运行时环境(版本1.8.0_121-b13)Java HotSpot(TM)64位服务器VM(版本25.121-b13,混合模式)

java timezone timezone-offset java-time

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

在 log4j 2 中使用 throwable 和参数

我想用一些动态参数记录一个错误(使用 log4j 2),以便更好地理解出了什么问题,我面临的问题是没有像这样的方法:

void error(String message, Throwable t);
Run Code Online (Sandbox Code Playgroud)

带参数支持。

在我的代码中,我希望异常和参数都在消息中填充 {}:

    try {
        //...
    } catch (Exception e) {
        LOGGER.error("Error removing data for account {}", accountId, e);
    }
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来实现它而不是像这样使用它?

LOGGER.error("Error removing token for account " + accountId, e);
Run Code Online (Sandbox Code Playgroud)

java logging log4j exception log4j2

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

发送ISO8583请求时出现NullPointerException

我正在尝试使用以下代码向终端发送ISO8583请求:

try {
        XMLParser packager = new XMLParser(this.xmlFile);


        final ISOMsg isoMsg = new ISOMsg();
        isoMsg.setPackager(packager);
        isoMsg.setMTI("0800");
        isoMsg.set(3, "xxxxxx");
        isoMsg.set(7, "xxxxxxxxxx");    //MMDDhhmmss
        isoMsg.set(11, "xxxxxx");
        isoMsg.set(12, "084500");       //Time of the message HHmmss
        isoMsg.set(13, "0621");         //Date MMDD
        isoMsg.set(41, "xxxxxxxx");
        isoMsg.set(62,"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        isoMsg.set(63,"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        BaseChannel channel = new BaseChannel() {};
        channel.setLogger(logger, "server-request-channel");
        channel.setHeader(isoMsg.pack());
        channel.setHost("xx.xx.xx.xx", xxxxx);
        ISOMUX isoMux = new ISOMUX(channel) {
            @Override
            protected String getKey(ISOMsg m) throws ISOException {
                return super.getKey(m);
            }
        };

        isoMux.setLogger(logger, "server-request-mux");
        new Thread(isoMux).start();
        ISORequest req = new ISORequest(isoMsg);
        isoMux.queue(req);
        req.setLogger(logger, "server-request-logger");
        ISOMsg …
Run Code Online (Sandbox Code Playgroud)

java android jpos iso8583

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

使用具有关系的Spring Data REST发布实体

我正在使用Spring Data Rest.尝试使用关联POST对象时遇到问题(例如,地址是我的实体中的一个字段,映射为多个对象).

问题是,我们应该使用什么格式将我们的新实体与其关系联系起来.我看到了几个答案并尝试了我找到的所有选项.不幸的是,所有这些都不适合我.发生以下错误:

Caused by: org.h2.jdbc.JdbcSQLException: NULL not allowed for column "ADDRESS_ID"; SQL statement:
Run Code Online (Sandbox Code Playgroud)

我尝试过的JSON:

{
"name": "test",
"email": "test@email",
"address": "http://localhost:8080/MyApp/address/1"
}
Run Code Online (Sandbox Code Playgroud)

还试过这些:

"address": {"id":"http://localhost:8080/MyApp/address/1"}
Run Code Online (Sandbox Code Playgroud)

还有这个:

"address":{"id":1}
Run Code Online (Sandbox Code Playgroud)

甚至这个:

"address": {
"href": "http://localhost:8080/MyApp/address/1"
}
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点,或者只为POST编写自己的控制器实现?谢谢!

java spring hibernate spring-data-rest

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

杰克逊JSR310中缺少ZonedDateTimeDeserializer

我正在解析ZonedDateTime这样的用法:

 @JsonSerialize(using = ZonedDateTimeSerializer.class)
 private ZonedDateTime expirationDateTime;
Run Code Online (Sandbox Code Playgroud)

我需要能够正确反序列化这个日期。但是,杰克逊没有为此提供反序列化器:

com.fasterxml.jackson.datatype.jsr310.deser
Run Code Online (Sandbox Code Playgroud)

有什么原因会丢失它吗?最常见的解决方法是什么?

更新:这是我的情况:

我这样创建ZonedDateTime

ZonedDateTime.of(2017, 1, 1, 1, 1, 1, 1, ZoneOffset.UTC)
Run Code Online (Sandbox Code Playgroud)

然后我序列化包含日期的对象,如下所示:

public static String toJSON(Object o) {
    ObjectMapper objectMapper = new ObjectMapper();
    StringWriter sWriter = new StringWriter();
    try {
        JsonGenerator jsonGenerator = objectMapper.getJsonFactory().createJsonGenerator(sWriter);
        objectMapper.writeValue(jsonGenerator, o);
        return sWriter.toString();
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试将其发送到Spring MVC Controller时:

    mockMvc.perform(post("/endpoint/")
            .content(toJSON(myObject))
            .contentType(APPLICATION_JSON))
            .andExpect(status().isOk());
Run Code Online (Sandbox Code Playgroud)

控制器内部的日期对象不同。

之前: 2017-01-01T01:01:01.000000001Z

后: 2017-01-01T01:01:01.000000001Z[UTC]

java jaxb date-parsing jackson zoneddatetime

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