我创建了一个简单的 java 应用程序作为我的大学迷你项目,其中的模块之一允许用户执行插入、删除、更新和搜索等操作。
出于验证目的,如果用户尝试删除数据库中不存在的记录(例如“抱歉未找到记录”),我希望向用户显示一条错误消息。
我尝试使用try catchblock 来检查 mongodb 是否在未找到文档时抛出异常,但这不起作用。我是Java和Mongodb新手,需要帮助。
这是我的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块不会生成未找到类型异常。
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) 我正在尝试使用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) 我想问您是否(以及如何)可以在 Red Hat JBoss Developer Studio 10.3.0.GA 中使用 Eclipse Marketplace。我试图从 [1] 安装 EMPC,但该版本看起来有点旧,之后无法运行。基本上我想将 [2] 安装到 Devstudio。我怎样才能做到这一点?我正在尝试这种拖放,但它对我不起作用。
谢谢
我在 Typescript 中有一个包含地图的类:
public map:Map<string,string>;
constructor() {
let jsonString = {
"peureo" : "dsdlsdksd"
};
this.map = jsonString;
}
Run Code Online (Sandbox Code Playgroud)
问题是初始化不起作用。
jsonString是我将从序列化为 Json 的 Java 对象收到的东西。
有人可以帮忙吗?谢谢!
我是一名 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 方法不会覆盖或实现超类型中的方法
有谁知道如何修复?
我想发送这样的请求:
我有一个错误:
“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) 使用此配置时,我收到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 模块?
我有一个将双精度数转换为字符串的转换器。在转换之前,我想将双精度格式格式化为固定的小数位数。但我注意到它没有被调用。这是我的方法:
我的两个模型具有相同的属性名称。
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将我映射Double到String罚款。
但它不遵守格式化的小数位。知道我缺少什么吗?
我有一个标准的 SNS 主题,并且我\xc2\xb4ve 设置了“订阅过滤策略”,如下所示:
\n{\n "event": [\n "eventName"\n ]\n}\nRun Code Online (Sandbox Code Playgroud)\n当我使用属性消息通过 AWS 控制台发布消息时,该消息将发送至正确的 SQS 订阅者。所以订阅过滤器工作得很好。
\n现在我正在尝试对我的 java 代码(Spring Boot)执行相同的操作。
\n我正在使用该库spring-cloud-aws-messaging,据我所知它只是 AWS JDK 的包装器。
问题是我不知道如何设置消息属性,就像我在 AWS 控制台上所做的那样。
\n\xc2\xb4 与我发送到 SNS 的 JSON 格式无关,属性始终位于 SNS 消息的正文中。我想有一个特定的方法来设置这些属性。
\n我发现com.amazonaws.services.sns.model.MessageAttributeValue
我不确定是否是正确的类,而且我无法理解如何发布消息和属性,因为发布方法不接受它。
\nthis.amazonSNS.publish(this.snsTopicARN, message, messageAttributes ???);\nRun Code Online (Sandbox Code Playgroud)\n java amazon-web-services amazon-sns spring-cloud spring-cloud-aws
我希望从以下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) 我正在尝试从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 ×7
javascript ×2
spring ×2
spring-boot ×2
amazon-sns ×1
cassandra ×1
converters ×1
date ×1
eclipse ×1
enums ×1
gradle ×1
html ×1
jhipster ×1
jquery ×1
jsf-2 ×1
json ×1
junit ×1
junit4 ×1
junit5 ×1
localdate ×1
model ×1
modelmapper ×1
mongodb ×1
primefaces ×1
spring-cloud ×1
spring-data ×1
truncate ×1
try-catch ×1
typescript ×1