我的项目有Spring Security.主要问题:无法访问http:// localhost:8080/api/v2/api-docs中的 swagger URL .它表示缺少或无效的授权标头.
浏览器窗口的屏幕截图 My pom.xml包含以下条目
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
SwaggerConfig:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfo("My REST API", "Some custom description of API.", "API TOS", "Terms of service", "myeaddress@company.com", "License of API", "API license URL");
return apiInfo;
}
Run Code Online (Sandbox Code Playgroud)
AppConfig的:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.musigma.esp2" })
@Import(SwaggerConfig.class) …
Run Code Online (Sandbox Code Playgroud) 这是针对init binder的互联网代码
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
Run Code Online (Sandbox Code Playgroud)
谁能解释一下:
1)为什么使用它,我的意思是,之前的问题是什么,它是如何通过该功能解决的.所以我想知道这个日期格式解决的原始日期有什么问题?
2)如何从JSP表单的角度使用这种格式,我的意思是,如果我们以文本格式输入日期,它是否会转换为特定格式然后保存它?
3)它是如何应用格式化的,我的意思是,我们必须在对象类中做一些事情吗?
我目前的代码:
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
Mall[] malls = restTemplate.getForObject(url, Mall[].class);
Run Code Online (Sandbox Code Playgroud)
我需要在表单中为我的请求添加一些自定义标头:
X-TP-DeviceID : <GUID>
Run Code Online (Sandbox Code Playgroud)
在我的案例中,最简单的方法是什么?restTemplate
在将请求发送到服务器之前,有没有办法向我的对象添加自定义标头定义?
[编辑]
这是对的吗?
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
HttpHeaders headers = new HttpHeaders();
headers.set("X-TP-DeviceID", "1234567890");
HttpEntity entity = new HttpEntity(headers);
HttpEntity<Mall[]> response = restTemplate.exchange(url, HttpMethod.GET, entity, Mall[].class);
Mall[] malls = response.getBody();
Run Code Online (Sandbox Code Playgroud)
[添加]
所以,我设法让它工作.但是,我对此并不完全满意.在我的情况下,我将需要为我所做的所有调用提供相同的自定义标头.
所以,我的下一个问题是 - 是否可以设置我的自定义标头在每次web-service
调用时自动添加,例如,通过扩展RestTemplate
类并将所有自定义标头放在那里?然后,我需要做的就是简单地使用我的自定义扩展RestTemplate
而不是库存扩展,默认情况下我的所有自定义标头都会出现在那里.
我的方法之一中有以下代码
ZonedDateTime current = Instant.now().atZone(ZoneId.of(AMERICA_NEW_YORK));
Run Code Online (Sandbox Code Playgroud)
我想current
在 JUnit 测试中进行模拟。
我尝试过,java.time.Clock
但为此,我需要将其添加到类构造函数中,因为我的代码写入旧版本的 Spring 并使用基于 XML 的配置,此类会导致问题,因为它需要 application-context.xml 文件中的构造函数参数,如果我使用构造函数与Clock
.
有没有办法避免current
上面代码中的构造函数配置和模拟。
更新
根据帕维尔·斯米尔诺夫的评论,我在下面尝试过,但current
仍然返回今天的日期,但不是我嘲笑的日期。
ZonedDateTime exactOneDay = ZonedDateTime.parse("Sun Oct 21 12:30:00 EDT 2018", Parser);
doReturn(exactOneDay).when(spyEmployeeHelper).getCurrentTime();
employee = getEmployees().get(0);
assertEquals(Integer.valueOf(1), employee.getNoticePeriod());
Run Code Online (Sandbox Code Playgroud) 我正在处理将数据发送到的应用程序zeromq
.以下是我的应用程序:
SendToZeroMQ
将数据发送到zeromq.retryQueue
同一个类中,以便以后可以在未收到确认的情况下重试.它使用带有maximumSize限制的guava缓存.SendToZeroMQ
则将重试发送相同的数据.如果收到确认,那么我们将把它删除,retryQueue
以便不能再次重试.想法很简单,我必须确保我的重试策略正常工作,这样我就不会丢失我的数据.这是非常罕见的,但如果我们没有收到acknolwedgements.
我正在考虑构建两种类型,RetryPolicies
但我无法理解如何在这里构建与我的程序相对应的:
RetryNTimes:
在此,它将在每次重试之间以特定的睡眠重试N次,之后,它将丢弃记录.ExponentialBackoffRetry:
在此,它将指数级地继续重试.我们可以设置一些最大重试限制,然后它将不会重试并将丢弃记录.下面是我的SendToZeroMQ
类,它将数据发送到zeromq,也从后台线程每30秒重试一次并启动ResponsePoller
runnable,它会一直运行:
public class SendToZeroMQ {
private final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(5);
private final Cache<Long, byte[]> retryQueue =
CacheBuilder
.newBuilder()
.maximumSize(10000000)
.concurrencyLevel(200)
.removalListener(
RemovalListeners.asynchronous(new CustomListener(), executorService)).build();
private static class Holder {
private static final SendToZeroMQ INSTANCE = new SendToZeroMQ();
}
public static SendToZeroMQ getInstance() {
return Holder.INSTANCE;
}
private SendToZeroMQ() {
executorService.submit(new ResponsePoller());
// retry …
Run Code Online (Sandbox Code Playgroud) 如何使用 Spring Bootwebclient
发布application/x-www-form-urlencoded
内容类型为“application/x-www-form-urlencoded”的示例 curl 请求的请求
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'username=XXXX' \
--data-urlencode 'password=XXXX'
Run Code Online (Sandbox Code Playgroud)
如何使用 webclient 发送相同的请求?
我尝试在静态网站上添加SSL/TLS.我使用Gitlab静态页面,而Jekyll用于内容.
我按照这个说明设置TLS - Gitlab教程.
我在这部分堆栈 - 我从Gitlab页面得到404错误
构建完成后,如果一切正常,请再次测试:
Run Code Online (Sandbox Code Playgroud)# Note that we're using the actual domain, not localhost anymore $ curl http://YOURDOMAIN.org/.well-known/acme-challenge/5TBu788fW0tQ5EOwZMdu1Gv3e9C33gxjV58hVtWTbDM
问题是接下来我用命令成功生成证书./letsencrypt-auto certonly -a manual -d example.com
我letsencrypt-setup.html
在根目录中创建了自定义页面whit适当的内容.
我运行jekyll build
命令,它创建了_site/.well-known/acme-challenge/5TBu788fW0tQ5EOwZMdu1Gv3e9C33gxjV58hVtWTbDM.html
页面.
当我curl
向这个页面运行命令时,它可以使用和不使用.html
扩展 - 两个命令都可以工作,并返回适当的值
curl http://localhost:4000/.well-known/acme-challenge/5TBu788fW0tQ5EOwZMdu1Gv3e9C33gxjV58hVtWTbDM
curl http://localhost:4000/.well-known/acme-challenge/5TBu788fW0tQ5EOwZMdu1Gv3e9C33gxjV58hVtWTbDM.html
Run Code Online (Sandbox Code Playgroud)
当我在构建和部署之后提交更改并推送到Gitlab时,我只能使用第二个命令获取适当的内容
curl http://example.com/.well-known/acme-challenge/5TBu788fW0tQ5EOwZMdu1Gv3e9C33gxjV58hVtWTbDM.html
Run Code Online (Sandbox Code Playgroud)
当我跑
curl http://example.com/.well-known/acme-challenge/5TBu788fW0tQ5EOwZMdu1Gv3e9C33gxjV58hVtWTbDM
Run Code Online (Sandbox Code Playgroud)
我收到404错误.
如果我按./letsencrypte
脚本继续我也有404错误.此工具尝试不带.html
扩展名的URL .
我阅读了教程中的注释并尝试了这种解决方法,但它对我不起作用.
我不知道下一步该尝试什么 - 我对Jekyll/SSL没有多少经验
我有两个数组映射.
Map<String, List<String>> map1 = new HashMap<>();
Map<String, List<String>> map2 = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)
我想将它们合并到一个新地图中.
如果两个映射中都存在密钥,那么我应该合并数组.
例如:
map1.put("k1", Arrays.asList("a0", "a1"));
map1.put("k2", Arrays.asList("b0", "b1"));
map2.put("k2", Arrays.asList("z1", "z2"));
// Expected output is
Map 3: {k1=[a0, a1], k2=[b0, b1, z1, z2]}
Run Code Online (Sandbox Code Playgroud)
我尝试用流来做到这一点
Map<String, List<String>> map3 = Stream.of(map1, map2)
.flatMap(map -> map.entrySet().stream())
.collect(Collectors.toMap(
Map.Entry::getKey,
e -> e.getValue().stream().collect(Collectors.toList())
));
Run Code Online (Sandbox Code Playgroud)
如果地图中没有相同的键,则此工作.否则,我得到例外
Exception in thread "main" java.lang.IllegalStateException: Duplicate key k2 (attempted merging values [b0, b1] and [z1, z2])
at java.base/java.util.stream.Collectors.duplicateKeyException(Collectors.java:133)
at java.base/java.util.stream.Collectors.lambda$uniqKeysMapAccumulator$1(Collectors.java:180)
at java.base/java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
at java.base/java.util.HashMap$EntrySpliterator.forEachRemaining(HashMap.java:1751)
at java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:658) …
Run Code Online (Sandbox Code Playgroud) 如何将以下 cURL 命令转换为 Postman rest 调用?
curl -X POST abc.com/input.import
-H 'content-type: application/x-www-form-urlencoded'
--data-urlencode "apiKey=123-456"
--data-urlencode "secret=12/her"
--data-urlencode "userKey=ApUR"
--data-urlencode "email=fakeImportedAccount@example.com"
--data-urlencode "profile={'firstName':'John','lastName':'Kira'}"
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法:
网址:(POST)abc.com/input.import
标题:内容类型:应用程序/json
身体:
{
"apiKey":"123-456",
"userKey":"ApUR",
"secret":"12/her",
"email":"fakeImportedAccount@example.com",
"profile": {
"firstName":"John",
"lastName":"Kira"
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:需要 Postman 中的原始正文格式。导入以"x-www-form-urlencoded"
表单形式创建请求
我已经使用 snap 安装了 IntelliJ IDEA,并且我已经安装了边缘版本:
sudo snap install intellij-idea-ultimate --edge --classic
当前状态:
$ snap list
Name Version Rev Tracking Developer Notes
atom 1.27.2 159 stable snapcrafters classic
core 16-2.33 4830 stable canonical core
intellij-idea-ultimate 2018.2-EAP 56 edge jetbrains classic
...
Run Code Online (Sandbox Code Playgroud)
如何在不破坏当前项目设置和破坏许可证的情况下用稳定版本替换边缘版本?
java ×6
java-8 ×2
rest ×2
spring-mvc ×2
binding ×1
curl ×1
gitlab ×1
gitlab-ci ×1
guava ×1
hashmap ×1
jekyll ×1
junit ×1
lets-encrypt ×1
postman ×1
spring ×1
spring-boot ×1
springfox ×1
ssl ×1
swagger ×1
swagger-2.0 ×1
swagger-ui ×1