所以我想通过DropWizard资源更改用于验证模型的验证消息.
我正在使用java bean验证注释.例如,这是我要验证的字段之一:
@NotEmpty(message = "Password must not be empty.")
Run Code Online (Sandbox Code Playgroud)
我可以使用验证器按预期测试它的工作原理.
但是,当我使用DropWizard对资源进行验证时,它会为该消息添加一些额外的内容.我看到的是这个 - password Password must not be empty. (was null)我发现这里的代码 - https://github.com/dropwizard/dropwizard/blob/master/dropwizard-validation/src/main/java/io/dropwizard/validation/ ConstraintViolations.java
具体这个方法 -
public static <T> String format(ConstraintViolation<T> v) {
if (v.getConstraintDescriptor().getAnnotation() instanceof ValidationMethod) {
final ImmutableList<Path.Node> nodes = ImmutableList.copyOf(v.getPropertyPath());
final ImmutableList<Path.Node> usefulNodes = nodes.subList(0, nodes.size() - 1);
final String msg = v.getMessage().startsWith(".") ? "%s%s" : "%s %s";
return String.format(msg,
Joiner.on('.').join(usefulNodes),
v.getMessage()).trim();
} else {
return String.format("%s %s (was %s)",
v.getPropertyPath(),
v.getMessage(),
v.getInvalidValue());
}
}
Run Code Online (Sandbox Code Playgroud)
有什么办法可以覆盖这种行为吗?我只想显示我在注释中设置的消息...
我正在尝试从C#运行一个powershell脚本.我没有问题将字符串传递给脚本但是当我尝试将数组传递给powershell脚本时,会抛出异常.这是C#代码:
string [] test = {"1","2","3","4"};
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.ApartmentState = System.Threading.ApartmentState.STA;
runspace.ThreadOptions = PSThreadOptions.UseCurrentThread;
runspace.Open();
RunspaceInvoke invoker = new RunspaceInvoke();
invoker.Invoke("Set-ExecutionPolicy Unrestricted");
Pipeline pipeline = runspace.CreatePipeline();
Command myCmd = new Command(@"C:\test.ps1");
CommandParameter param = new CommandParameter("responseCollection", test);
myCmd.Parameters.Add(param);
pipeline.Commands.Add(myCmd);
// Execute PowerShell script
Collection<PSObject> results = pipeline.Invoke();
Run Code Online (Sandbox Code Playgroud)
这是powershell脚本:
param([string[]] $reponseCollection)
$a = $responseCollection[0]
Run Code Online (Sandbox Code Playgroud)
每次执行此代码时抛出:
Cannot index into a null array.
Run Code Online (Sandbox Code Playgroud)
我知道在将字符串传递给powershell脚本时执行powershell脚本的代码是正确的,它已经过全面测试.
我有以下代码,我测试和工作:
using (new Impersonator("Administrator", "dev.dev", #########"))
{
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
Pipeline pipeline = runspace.CreatePipeline();
Command myCmd = new Command(@"C:\test.ps1");
myCmd.Parameters.Add(new CommandParameter("upn", upn));
myCmd.Parameters.Add(new CommandParameter("sipAddress", sipAddress));
pipeline.Commands.Add(myCmd);
// Execute PowerShell script
Collection<PSObject> results = pipeline.Invoke();
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试将该函数包含在另一个项目中,以便从Web服务调用它时,它会抛出一个execption:
System.Management.Automation.CmdletInvocationException: Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied. ---> System.UnauthorizedAccessException: Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied.
Run Code Online (Sandbox Code Playgroud)
我不知道为什么会这样.任何帮助,将不胜感激.