小编ℛɑƒ*_*ƒæĿ的帖子

使用Mongodb和Java检查文档是否存在?

我创建了一个简单的 java 应用程序作为我的大学迷你项目,其中的模块之一允许用户执行插入、删除、更新和搜索等操作。

出于验证目的,如果用户尝试删除数据库中不存在的记录(例如“抱歉未找到记录”),我希望向用户显示一条错误消息。

我尝试使用try catchblock 来检查 mongodb 是否在未找到文档时抛出异常,但这不起作用。我是JavaMongodb新手,需要帮助。

这是我的deleteActionPerformed代码和我尝试过的代码:

private void deleteActionPerformed(java.awt.event.ActionEvent evt) {
    try {
        // my collection name is activity
        DBCollection col = db.getCollection("activity");
        // Tid is the TextField in which i am taking input of _id
        if(!Tid.getText().equals("")) {
            col.remove(new BasicDBObject().append("_id",(Object)Tid.getText()));
        } else {
            JOptionPane.showMessageDialog(null,"Please Enter the ID");
        }
    } catch(Exception e){
        JOptionPane.showMessageDialog(null,"Record not Found " + e);
    }
}
Run Code Online (Sandbox Code Playgroud)

try catch块不会生成未找到类型异常。

java try-catch mongodb

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

字体大小根据单词的数量而变化

function test(data) {
  wordCount = {};
  theWords = [];
  allWords = data.match(/\b\w+\b/g); //get all words in the document

  for (var i = 0; i < allWords.length; i = i + 1) {
    allWords[i] = allWords[i].toLowerCase();
    var word = allWords[i];
    if (word.length > 5) {
      if (wordCount[word]) {
        wordCount[word] = wordCount[word] + 1;
      } else {
        wordCount[word] = 1;
      }
    }
  }
  var theWords = Object.keys(wordCount); // all words over 5 characters
  var result = "";
  for (var i = 0; …
Run Code Online (Sandbox Code Playgroud)

html javascript jquery

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

如何使用Springdata将Enum存储到Cassandra中?

我正在尝试使用Springdata框架将枚举存储到Cassandra中.这可能吗?我已经定义了我的枚举:

public enum Currency {
     GBP, 
     USD, 
     EUR
}
Run Code Online (Sandbox Code Playgroud)

然后我在字段上使用"@Enumerated"注释定义我的类:

@Table
public class BondStatic {

    @PrimaryKey
    private String id;

    private String isin;
    private String ticker;
    private Date maturity;
    private Double coupon;
    @Enumerated(EnumType.STRING)
    private Currency currency;
    ...
}
Run Code Online (Sandbox Code Playgroud)

这些是我的进口:

import com.datastax.driver.mapping.EnumType;
import com.datastax.driver.mapping.annotations.Enumerated;
Run Code Online (Sandbox Code Playgroud)

然后我有Spring的Component类:

@Component
class CassandraDataCLR implements CommandLineRunner {

    @Autowired
    public BondStaticCassandraRepository bondStaticRepository;
    ...
}
Run Code Online (Sandbox Code Playgroud)

哪个是自动装配的:

@RepositoryRestResource(path = "/bond-static")
interface BondStaticCassandraRepository extends CassandraRepository<BondStatic> { }
Run Code Online (Sandbox Code Playgroud)

这些是我的进口:

import org.springframework.data.cassandra.repository.CassandraRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
Run Code Online (Sandbox Code Playgroud)

但是当我尝试运行以下内容时:

bondStaticRepository.save(bondStatics);
Run Code Online (Sandbox Code Playgroud)

我明白了:

public enum Currency {
     GBP, 
     USD, 
     EUR …
Run Code Online (Sandbox Code Playgroud)

java enums cassandra spring-data

5
推荐指数
0
解决办法
2063
查看次数

在 Red Hat JBoss Developer Studio (Devstudio) 中使用 Eclipse Marketplace Client

我想问您是否(以及如何)可以在 Red Hat JBoss Developer Studio 10.3.0.GA 中使用 Eclipse Marketplace。我试图从 [1] 安装 EMPC,但该版本看起来有点旧,之后无法运行。基本上我想将 [2] 安装到 Devstudio。我怎样才能做到这一点?我正在尝试这种拖放,但它对我不起作用。

谢谢

[1] http://download.eclipse.org/mpc/neon

[2] https://marketplace.eclipse.org/content/angular-ide

eclipse eclipse-marketplace jboss-developer-studio

5
推荐指数
2
解决办法
2166
查看次数

从 json 对象初始化 Typescript 中的 Map&lt;string,string&gt;

我在 Typescript 中有一个包含地图的类:

public map:Map<string,string>;
constructor() {
  let jsonString = {
    "peureo" : "dsdlsdksd"
  };
  this.map = jsonString;
}
Run Code Online (Sandbox Code Playgroud)

问题是初始化不起作用。

jsonString是我将从序列化为 Json 的 Java 对象收到的东西。

有人可以帮忙吗?谢谢!

javascript json initialization typescript

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

org.springframework.boot.actuate.autoconfigure.metrics.MetricsEndpointAutoConfiguration 上的错误处理条件

我是一名 Java Spring 新手程序员。我正在将一些测试代码从旧的 jHipster 项目移动到新项目。我将其添加到 pom.xml 中以修复编译错误。这解决了我的编译问题,但导致了运行时错误。

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-actuator</artifactId>
   <version>1.5.7.RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

我现在收到这些运行时错误。

引起的原因:java.lang.IllegalStateException:org.springframework.boot.actuate.autoconfigure.metrics.MetricsEndpointAutoConfiguration的处理条件错误引起的:java.lang.IllegalArgumentException:找不到类[org.springframework.boot.actuate.metrics。指标端点]

 <spring-boot.version>2.2.5.RELEASE</spring-boot.version>
Run Code Online (Sandbox Code Playgroud)

如果我删除 spring-boot-actuator

不兼容的类型: java.time.Instant 无法转换为 java.util.Date 无法访问 org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter 方法不会覆盖或实现超类型中的方法

有谁知道如何修复?

java spring spring-boot jhipster

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

使用 LocalDate 类型参数发送请求

我想发送这样的请求:

  • http://localhost:8080/reports/daily?start_date=2018-03-22&end_date=2018-03-24

我有一个错误:

“2018-05-31 15:40:29.623 WARN 11496 --- [nio-8080-exec-5] .wsmsDefaultHandlerExceptionResolver:无法绑定请求元素:org.springframework.web.method.annotation.MethodArgumentTypeMismatchException:无法转换'java.lang.String' 类型到所需类型 'java.time.LocalDate';嵌套异常是 org.springframework.core.convert.ConversionFailedException:无法从类型 [java.lang.String] 转换为类型 [@org .springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat java.time.LocalDate] for value '2018-03-22';嵌套异常是 java.lang.IllegalArgumentException:解析尝试失败的值[2018-03-22]》

问题很明显,所以我找到了解决方案并改变了一切。我的方法:

@RequestMapping(path = "reports/daily", method = RequestMethod.GET, 
                consumes = MediaType.APPLICATION_JSON_VALUE)
public String getDailyReport(@RequestParam ("start_date") 
                             @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) 
                             LocalDate startDate, 
                             @RequestParam("end_date") 
                             @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) 
                             LocalDate endDate) {
    double totalDistance = 0.0;
    double totalPrice = 0.0;
    List<Transit> transits = transitService.getTransits(startDate, endDate);
    for (Transit transit : transits) {
        if (transit.getDistance() != null && transit.getPrice() != null) {
            try {
                totalDistance …
Run Code Online (Sandbox Code Playgroud)

java spring date spring-boot localdate

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

如何在 Gradle 构建文件中正确排除“junit-vintage-engine”?

使用此配置时,我收到initializationErrorJUnit Vintage 的:

testImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test'
Run Code Online (Sandbox Code Playgroud)

但使用此配置@RunWith注释无法解决:

testImplementation (group: 'org.springframework.boot', name: 'spring-boot-starter-test') {
    exclude group: 'org.junit.vintage', name: 'junit-vintage-engine', version: '5.7.0-M1'
}
Run Code Online (Sandbox Code Playgroud)

如何正确排除 JUnit Vintage 模块?

junit junit4 gradle junit5 junit-vintage

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

ModelMapper 中的自定义转换器不起作用

我有一个将双精度数转换为字符串的转换器。在转换之前,我想将双精度格式格式化为固定的小数位数。但我注意到它没有被调用。这是我的方法:

我的两个模型具有相同的属性名称。

private String price;  // my DTO
private Double price;  // my JPA entity
Run Code Online (Sandbox Code Playgroud)

这是我的 modelMapper bean ModelMapperConfig.java

public ModelMapper modelMapper() {
  ModelMapper modelMapper = new ModelMapper();
  modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
  modelMapper.getConfiguration().setAmbiguityIgnored(true);
  modelMapper.addConverter(convertDoubleToString());
  return modelMapper;
}

private Converter<Double, String> convertDoubleToString() {
    return mappingContext -> Utility.formatDoubleToString(mappingContext.getSource());
}
Run Code Online (Sandbox Code Playgroud)

正在ModelMapper将我映射DoubleString罚款。

但它不遵守格式化的小数位。知道我缺少什么吗?

java model converters modelmapper

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

如何使用 spring-cloud-aws-messaging 或 aws-java-sdk 设置 AWS SNS 消息属性值?

我有一个标准的 SNS 主题,并且我\xc2\xb4ve 设置了“订阅过滤策略”,如下所示:

\n
{\n  "event": [\n    "eventName"\n  ]\n}\n
Run Code Online (Sandbox Code Playgroud)\n

当我使用属性消息通过 AWS 控制台发布消息时,该消息将发送至正确的 SQS 订阅者。所以订阅过滤器工作得很好。

\n

现在我正在尝试对我的 java 代码(Spring Boot)执行相同的操作。

\n

我正在使用该库spring-cloud-aws-messaging,据我所知它只是 AWS JDK 的包装器。

\n

问题是我不知道如何设置消息属性,就像我在 AWS 控制台上所做的那样。

\n

\xc2\xb4 与我发送到 SNS 的 JSON 格式无关,属性始终位于 SNS 消息的正文中。我想有一个特定的方法来设置这些属性。

\n

我发现com.amazonaws.services.sns.model.MessageAttributeValue

\n

我不确定是否是正确的类,而且我无法理解如何发布消息和属性,因为发布方法不接受它。

\n
this.amazonSNS.publish(this.snsTopicARN, message, messageAttributes ???);\n
Run Code Online (Sandbox Code Playgroud)\n

java amazon-web-services amazon-sns spring-cloud spring-cloud-aws

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

在PF3.1中使用onRowKey(p:datatable)传递多个参数

我希望从以下Primefaces 3.1 p:datatable传递多个参数:

 <p:dataTable value="#{tableBean.carsModel}" var="var" rowkey="#{var.model}" 
              selection="#{tableBean.car}" selectionMode="single">
    <p:ajax event="rowSelect" listener="#{tableBean.onRowClick}"></p:ajax>
    <p:column>
        <f:facet name="header">
            <h:outputText styleClass="outputText" value="Model"></h:outputText>
        </f:facet>
        <h:outputText styleClass="outputText" value="#{var.model}"></h:outputText>
    </p:column>
    <p:column>
        <f:facet name="header">
            <h:outputText styleClass="outputText" value="Color"></h:outputText>
        </f:facet>
        <h:outputText styleClass="outputText" value="#{var.randomColor}"></h:outputText>
    </p:column>
</p:dataTable>
Run Code Online (Sandbox Code Playgroud)

我有一种情况,我正在使用多个键作为主键, rowkey="#{var.model}但是,如何使用多个主键。

我也在CarDataModel extends ListDataModel<Car> implements SelectableDataModel<Car>{ 上课。有人可以告诉我如何合作吗?

@Override
public Car getRowData(String rowKey) {
    //In a real app, a more efficient way like a query by rowKey 
    //should be implemented to deal with huge data
    List<Car> cars = (List<Car>) getWrappedData();
    for(Car car : …
Run Code Online (Sandbox Code Playgroud)

primefaces jsf-2

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

Java LocalDateTime 从 UTC 时区中删除毫秒

我正在尝试从UTC时区截断毫秒。

我有下面的代码,我可以删除毫秒,但Z最后我仍然得到。

OffsetDateTime now = OffsetDateTime.now(ZoneOffset.UTC );
OffsetDateTime eventDateTime=now.minus(4, ChronoUnit.MINUTES);

System.out.println("====Event date Time before truncate===");
System.out.println(eventDateTime);
      
System.out.println("====Event date Time after truncate===");
System.out.println(eventDateTime.truncatedTo(ChronoUnit.SECONDS));
Run Code Online (Sandbox Code Playgroud)

输出如下:

====事件日期截断前的时间===

2021-03-09T20:46:24.081Z

====事件日期截断后的时间===

2021-03-09T20:46:24Z

java truncate localdatetime

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