我在许多网站上阅读过Optional只能用作返回类型,而不能在方法参数中使用.我很难找到合乎逻辑的原因.例如,我有一个逻辑,它有2个可选参数.因此,我认为像这样编写我的方法签名是有意义的(解决方案1):
public int calculateSomething(Optional<String> p1, Optional<BigDecimal> p2 {
// my logic
}
Run Code Online (Sandbox Code Playgroud)
许多网页指定Optional不应该用作方法参数.考虑到这一点,我可以使用以下方法签名并添加一个清晰的Javadoc注释来指定参数可能为null,希望将来的维护者将读取Javadoc,因此在使用参数之前始终执行空值检查(解决方案2) :
public int calculateSomething(String p1, BigDecimal p2) {
// my logic
}
Run Code Online (Sandbox Code Playgroud)
或者,我可以用四种公共方法替换我的方法以提供更好的界面并使其更明显p1和p2是可选的(解决方案3):
public int calculateSomething() {
calculateSomething(null, null);
}
public int calculateSomething(String p1) {
calculateSomething(p1, null);
}
public int calculateSomething(BigDecimal p2) {
calculateSomething(null, p2);
}
public int calculateSomething(String p1, BigDecimal p2) {
// my logic
}
Run Code Online (Sandbox Code Playgroud)
现在我尝试编写类的代码,为每种方法调用这条逻辑.我首先从另一个返回Optionals的对象中检索两个输入参数然后调用calculateSomething.因此,如果使用解决方案1,则调用代码将如下所示:
Optional<String> p1 = otherObject.getP1();
Optional<BigInteger> p2 = otherObject.getP2();
int result = myObject.calculateSomething(p1, p2);
Run Code Online (Sandbox Code Playgroud)
如果使用解决方案2,则调用代码将如下所示:
Optional<String> …Run Code Online (Sandbox Code Playgroud) 我一直听到的声音Eclipse好于或远远超过,Visual Studio但是当我安装时,Eclipse我觉得它非常笨重且难以使用界面.所以我想知道什么是如此伟大Eclipse,如果有其他人同意我.
我也找不到一个类似的问题,谈论有关的具体特征Eclipse和它们的比较Visual Studio.
如何编写基于Mockito的JUnit方法来测试此方法adduser()?我尝试写一个,但它失败了一条错误消息,说不处理异常.显示错误:
when(service.addUser("nginx")).thenReturn("apache");
Run Code Online (Sandbox Code Playgroud)
假设addUser()业务类中的方法永远不会捕获任何异常,并且不会重新抛出.
class Business {
public User addUser() throws ServiceException{
User user = service.addUser("nginx");
return user;
}
}
Run Code Online (Sandbox Code Playgroud)
测试案例方法:
在测试类中,我使用@Mock属性模拟服务层类并注入它.
@Mock
Service service;
@InjectMocks
Business business = new Business();
@Test
public void testAddUser() {
when(service.addUser("nginx")).thenReturn("apache");
User user = business.addUser("nginx");
assertNotNull(user);
}
Run Code Online (Sandbox Code Playgroud)
请告诉我如何处理测试用例中的异常情况.
我有一个组合框,基本上select是一个由复杂对象数组填充的元素ng-options.当我在二级更新集合的任何对象时,此更改不会应用于组合框.
这也记录在AngularJS网站上:
请注意,
$watchCollection对象的属性(如果模型是数组,则为集合中的项)进行浅层比较.这意味着更改比对象/集合内的第一级更深的属性不会触发重新呈现.
<div ng-app="testApp">
<div ng-controller="Ctrl">
<select ng-model="selectedOption"
ng-options="(selectedOption.id + ' - ' + selectedOption.name) for selectedOption in myCollection track by selectedOption.id">
</select>
<button ng-click="changeFirstLevel()">Change first level</button>
<button ng-click="changeSecondLevel()">Change second level</button>
<p>Collection: {{ myCollection }}</p>
<p>Selected: {{ selectedOption }}</p>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
var testApp = angular.module('testApp', []);
testApp.controller('Ctrl', ['$scope', function ($scope) {
$scope.myCollection = [
{
id: '1',
name: 'name1',
nested: {
value: 'nested1'
}
}
]; …Run Code Online (Sandbox Code Playgroud) 我使用基本文件上传bootstrap-fileupload.js给我一个文件名和按钮,并希望用文本输入和按钮重复格式.
<div class="form-group">
<label class="control-label">Source File Name</label>
<div class="fileupload fileupload-new" data-provides="fileupload">
<div class="input-group">
<div class="form-control uneditable-input"><i class="icon-file fileupload-exists"></i>
</div>
<div class="input-group-btn">
<a class="btn btn-default btn-file">
<span class="fileupload-new">Select file</span>
<span class="fileupload-exists">Change</span>
<input type="file" class="file-input"/></a>
<a href="#" class="btn btn-default fileupload-exists" data-dismiss="fileupload">Remove</a>
</div>
</div>
</div>
</div>
<form id="frmOptions" method="post" class="form-inline span12">
<div class="row-fluid">
<div class="form-group">
<label class="control-label">File Name on Upload</label>
<div class="input-group ">
<input id="uploadname" type="text" class="form-control">
<a class="btn btn-default go inline">Upload to DocMan</a>
</div>
</div>
</div>
</form>
Run Code Online (Sandbox Code Playgroud)
页面托管在这里https://googledrive.com/host/0B90FGJizRd-gbm1JRUswYUY5dE0/bootstrap/ 我尝试了这种变化,没有任何效果.基本上我想要两个相似的全宽行,每行都有一个文本字段和一个按钮.欢迎任何建议.
我的应用程序有一个个人密钥库,其中包含用于本地网络的可信自签名证书 - 比如说mykeystore.jks.我希望能够使用已在本地配置的自签名证书连接到公共站点(例如google.com)以及本地网络中的站点.
这里的问题是,当我连接到https://google.com时,路径构建失败,因为设置我自己的密钥库会覆盖包含与JRE捆绑在一起的根CA的默认密钥库,报告异常
sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
Run Code Online (Sandbox Code Playgroud)
但是,如果我将CA证书导入我自己的密钥库(mykeystore.jks),它可以正常工作.有没有办法支持两者?
我有自己的TrustManger用于此目的,
public class CustomX509TrustManager implements X509TrustManager {
X509TrustManager defaultTrustManager;
public MyX509TrustManager(KeyStore keystore) {
TrustManagerFactory trustMgrFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustMgrFactory.init(keystore);
TrustManager trustManagers[] = trustMgrFactory.getTrustManagers();
for (int i = 0; i < trustManagers.length; i++) {
if (trustManagers[i] instanceof X509TrustManager) {
defaultTrustManager = (X509TrustManager) trustManagers[i];
return;
}
}
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
try …Run Code Online (Sandbox Code Playgroud) 在JavaFX TableView中,如何确定更改
将它们保存在首选项中并恢复它们,下次启动应用程序时?
I.列顺序
现在工作.但是,wasRemoved()在重新排序列时会触发,而不是wasPermutation().
final List<TableColumn<MyType, ?>> unchangedColumns = Collections.unmodifiableList(new ArrayList<TableColumn<MyType, ?>>(columns));
columns.addListener(new ListChangeListener<TableColumn<MyType, ?>>() {
@Override
public void onChanged(ListChangeListener.Change<? extends TableColumn<MyType, ?>> change) {
while (change.next()) {
if (change.wasRemoved()) {
ObservableList<TableColumn<MyType, ?>> columns = table.getColumns();
int[] colOrder = new int[columns.size()];
for (int i = 0; i < columns.size(); ++i) {
colOrder[i] = unchangedColumns.indexOf(columns.get(i));
}
// colOrder will now contain current order (e.g. 1, 2, 0, 5, 4)
}
}
}
}); …Run Code Online (Sandbox Code Playgroud) 拜托,谁能告诉我 Eclipse 在 (1) 和 (2) 附加图像之间描绘的区别是什么?

尝试使用谷歌搜索,但找不到上面说明的任何正确来源:)
我正在使用jQuery fileDownload(由John Culviner创建)来加载由JBoss服务器使用RESTEasy动态生成的DOCX文件.应用程序和文件位于同一个域中.
这适用于HTTP GET提取的文档.
但是,在一种情况下,我必须使用HTTP POST请求,因此我可以将额外的有效负载附加到请求.这也适用于我使用HTTP的本地开发环境.但是,在生产中,服务器由HTTPS保护.那里的文件下载不起作用.在浏览器控制台中,我收到文档URL 的错误403(禁止).
这是什么问题,我该如何解决?看起来浏览器无法访问资源,但我不确定这是客户端还是服务器问题.我是否必须在任何一方设置其他HTTP标头?或者这是JBoss配置问题?
由于我无法在本地重现问题,因此很难调试.
客户代码:
var downloadUrl = '/MyApp/foobar/download';
var downloadConfiguration = {
httpMethod: "POST",
data: JSON.stringify($scope.payload),
successCallback: onSuccess,
failCallback: onError
};
$.fileDownload(downloadUrl, downloadConfiguration);
Run Code Online (Sandbox Code Playgroud)
服务器代码:
@POST
@Path("/download")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces("text/word")
public Response generateDocument(final InputStream request) throws Exception {
// ...
Response.ResponseBuilder builder = null;
File file = createMyDocument();
builder = Response.ok((Object)file);
builder.header("Content-Disposition", String.format("attachment; filename=\"%s.%s\"", FILENAME, EXTENSION));
builder.header("Set-Cookie", "fileDownload=true; path=/");
return builder.build();
}
Run Code Online (Sandbox Code Playgroud)
编辑:
我也尝试过设置其他HTTP标头,但它没有帮助:
builder.header("X-Frame-Options", …Run Code Online (Sandbox Code Playgroud)