小编p.s*_*eef的帖子

RestRepositoryResource的自动配置

我想为实体实现RestRepositoryResource(以及一些额外的标准功能)的自动配置.我试图通过注释类@Configuration@SpringBootApplication注释类来实现它.

这样的事情:

@EnableRestRepo(single="foo", collection="foos",entity=Foo.class, id=String.class)
@SpringBootApplication
public class App{
    public void main(String[] args){
        SpringApplication.run(App.class,args);
    }
}


@Entity
public class Foo{
    String id;
    String bar;   
    ... getters & setters   
}
Run Code Online (Sandbox Code Playgroud)

这应该设置一个(或类似的功能,如果需要,我可以创建自己的端点),@RestRepositoryResource如下所示:

@RestRepositoryResource(itemResourceRel = "foo", collectionResourceRel = "foos")
public interface Repo extends CrudRepository<Foo,String> {

    @RestResource(rel = "foo")
    Foo findOneById(@Param("id") String id);
}
Run Code Online (Sandbox Code Playgroud)

这里的目标是在配置一些基本功能时减少一些锅炉板.显然,这个例子将扩展一些更自动配置的东西,但这应该以类似的方式工作.

问题不RestRepositoryResource在于关于使用需要参数和泛型类的注释的自动配置.我不介意花一些时间实现这个,但我不知道从哪里开始.

这样的事情是否可能,如果是这样,怎么样?

java spring spring-data-rest spring-boot

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

FFMPEG如何将MJPEG编码数据混合到mp4或avi容器中C++

我正在寻找一种将 mjpeg(压缩)视频数据混合到 mp4 或 avi 等视频容器中的方法。(将来我还需要添加音频)。由于我也在项目的其他部分使用 FFMPEG,如果可能的话,我想使用这些库来完成它。我不是在寻找命令行 FFMPEG 使用!我尝试使用 ffmpeg 中的多路复用示例,但我只能创建一个包含视频信息的(非常大的).mjpeg 文件。这不是我要找的。

示例将非常受欢迎,但指向正确方向的指针也有效!

编辑:我已将 yuvj422p 流输出到 jpeg 图像,我想将其放入 mp4 容器中。使用 ffmpeg 命令行可以正常工作:

ffmpeg -i yuvy%01d.jpg -vcodec mjpeg out.mp4 
Run Code Online (Sandbox Code Playgroud)

我想直接在我的代码中执行此操作(当然不首先创建 jpeg 图像)

ffmpeg mjpeg multiplexing

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

Java,如何确定printerjob是否已完成

我正在使用PDFBox和PrinterJob打印填充的PDF。我可以很好地创建和打印,但是使用PrinterJob似乎无法跟踪进度,甚至无法检查进度是否完成。

我发现我可以计算排队的打印机作业,但是没有名称/ ID,我无法将它们链接到正在打印的pdf。

我想知道是否有一种方法可以检查进度,或者至少知道特定的(命名)PrinterJob是否已完成。

public void print(PDDocument document, String printerName, boolean silentPrint) throws Exception
{
    PrintService printService = getPrinter(printerName);
    PrinterJob printJob = PrinterJob.getPrinterJob();

    printJob.setPrintService(printService);
    PDFPrintable pageable = new PDFPrintable(document,Scaling.ACTUAL_SIZE);
    PageFormat format = printJob.defaultPage();
    format.setOrientation(PageFormat.PORTRAIT);

    printJob.setPrintable(pageable, format);
    printJob.print();
}

private static PrintService getPrinter(String printerName) throws Exception
{

    PrintService[] services = PrinterJob.lookupPrintServices();
    PrintService myPrinter = null;
    if(services.length == 0)
        throw new Exception("Cannot find printer");
    for (int i = 0; i < services.length; i++)
    {
        String svcName = services[i].toString();  
        System.out.println("service found: "+svcName);
        if …
Run Code Online (Sandbox Code Playgroud)

java printing pdfbox

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

Jenkins Artifactory插件不生成POM文件

我正在詹金斯成功建立我的gradle项目并将罐子部署到神器中.但是,当我尝试在另一个项目中重用它们时,无法识别/下载其他依赖项.我认为这与神器中缺少的pom文件有关.

在神器中,JAR文件已正确上载,但旁边没有POM文件.

我是否理解我需要这个POM文件以便gradle在尝试再次下载时解决jar的依赖?为什么没有生成POM文件?

作业设置为:

Gradle-Artifactory整合

Project uses the Artifactory Gradle Plugin = false
Capture and publish build info = true
Publish artifacts to Artifactory = true
    Publish Maven descriptors = true
Use Maven compatible patterns = true
    patterns are default: [organization]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]
Run Code Online (Sandbox Code Playgroud)

建立:

use gradle wrapper = true
tasks = build
Run Code Online (Sandbox Code Playgroud)

artifactory gradle pom.xml maven jenkins

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

在jenkins管道脚本中获取gradle变量

我正在尝试使用groovy构建脚本将构建管道迁移到“ Pipeline插件”。

我的管道通常是:

  1. 测试(等级)
  2. IntegrationTest(等级)
  3. 构建(渐变)
  4. 发布(人工工厂)

我想在我的jenkins构建脚本中使用gradle变量,例如version / group等,以发布到工件中的正确文件夹。过去,人工插件会为我解决。如何做到这一点?

对于单个gradle项目,我使用如下代码:

node('master')
{
    def version = 1.0
    def gitUrl = 'some.git'
    def projectRoot = ""
    def group = "dashboard/frontend/"
    def artifactName = "dashboard_ui"
    def artifactRepo = "ext-release-local"

    stage "git"

    git branch: 'develop', poll: true, url: "${gitUrl}"

    dir(projectRoot)
    {

        sh 'chmod +x gradlew'
        stage "test"
        sh './gradlew clean test'

        stage "build"
        sh './gradlew build createPom'

        stage "artifact"
        def server = Artifactory.server('artifactory_dev01')
        def uploadSpec = """{
          "files": [
            {
              "pattern": "build/**.jar",
              "target": …
Run Code Online (Sandbox Code Playgroud)

groovy gradle jenkins jenkins-pipeline

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

当令牌不再有效时,Spring 安全 OAuth 身份验证不会过期

我正在使用 Spring security 和 spring oauth 在我的 Web 应用程序中进行身份验证(使用 jwt 令牌)。这工作正常,令牌被交换,我可以正确登录。但是,一旦登录,即使令牌过期,身份验证也不会过期。当我尝试重用令牌以从我的资源服务器获取资源时,它返回访问被拒绝,因为令牌不再有效。

我的网络应用程序是一个有状态(vaadin)网络应用程序。它使用会话来处理很多东西,我无法绕过它。使用 OAuth 进行身份验证后,它将使用“以前身份验证:org.springframework.security.oauth2.provider.OAuth2Authentication”来检查它是否已通过身份验证,我似乎这只会是“真”,直到会话被销毁。

我的 rest api 是状态/无会话的,因此每次都会正确验证令牌。如果过期,它将给出 401。

我发现处理这个问题的唯一方法是相当丑陋:如果 api 返回 401,则使会话无效。但是我想看到的是,Web 应用程序还会检查每个请求上令牌的有效性。使用会话时有没有办法做到这一点?

这是我的 webapp 的 oauth 安全配置部分。

@Configuration
@EnableOAuth2Sso
public class OAuthConfig extends WebSecurityConfigurerAdapter
{

    @Override
    protected void configure(HttpSecurity http) throws Exception
    {
        http.csrf().disable()
                .authorizeRequests()
                .antMatchers("/login**").permitAll()
                .anyRequest().authenticated()
                .and()
                .logout()
                .logoutSuccessUrl("/")
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .and()
                .exceptionHandling().authenticationEntryPoint((request, response, authException) -> response.sendRedirect("/login"));
    }

}
Run Code Online (Sandbox Code Playgroud)

spring-security spring-boot spring-security-oauth2

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

使用Spring Data存储库在Mongodb中获取最后创建的文档

我正在尝试在mongodb存储库中获取最后创建的项目的创建日期时间.

我显然可以使用findAll(排序排序)函数,并获取第一个元素,但这在大型数据库上不太实用.

Mongo查询不支持"orderBy"查询方法,因此这也不是解决方案.

创建的顺序是按照"创建"的时间顺序排列的,所以如果我可以在集合中获得最后创建的文档也是好的.

所以我的问题是:使用Spring数据在mongodb仓库中检索最后创建的文档的最佳方法是什么?

我目前的代码:

@Data
@Document
public class Batch
{
    @Id
    String id;
    LocalDateTime created;
    //other stuff
}


public interface BatchRepository extends MongoRepository<Batch,String>
{
    //this does not work
    //Batch findOneOrderByCreatedDesc();
}
Run Code Online (Sandbox Code Playgroud)

spring mongodb spring-data

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

Gradle artifactoryPublish将不会部署Spring Boot生成的.jar文件

我正在尝试使用gradle + artifactory插件+ maven-publish插件将jar + pom文件部署到工件中。

我试着像来自其他来源的多种解决方案这个,我觉得春天启动插件是打破东西(因为它编辑的jar文件)

以下脚本成功上传了.pom文件,但没有成功上传spring-boot生成的.jar文件。我如何才能将其也上传?

这是我的build.gradle:

buildscript {
    ext {
        springBootVersion = '1.4.0.RELEASE'
    }
    repositories {
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.4.0"
    }
}
apply plugin: 'java'
apply plugin: 'spring-boot'
apply plugin: "com.jfrog.artifactory"
apply plugin: 'maven-publish'
apply from: "gradle/artifactory.gradle"

publishing {
    publications {
        mavenJava(MavenPublication) {
            components.java
        }
    }
}

jar {
    baseName = 'BatchParser'

}

version = '1.0'
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {

    compile('org.projectlombok:lombok:1.16.10')
    ...
    testCompile('org.springframework.boot:spring-boot-starter-test') …
Run Code Online (Sandbox Code Playgroud)

artifactory gradle spring-boot

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

如何在 Spring Webflux 中将 java.time.Instant 序列化为 ISO 字符串

如何配置 webflux 将 java.time(特别是 Instant)序列化为 ISO 日期字符串?

使用下面的代码我得到:

{"id":"id","timestamp":1606890022.131813600}

我想得到

{"id":"id","timestamp":"2020-12-02T07:20:16.553Z"}

@EnableWebFlux
@SpringBootApplication
public class WebfluxJavaTimeSerializationApplication {

    public static void main(String[] args) {
        SpringApplication.run(WebfluxJavaTimeSerializationApplication.class, args);
    }
   
    @Bean
    public RouterFunction<?> routerFunction() {
        return route().GET("/resource", request -> ServerResponse.ok().body(Mono.just(new Resource()),Resource.class)).build();
    }

    public class Resource {
        String id = "id";
        Instant timestamp = Instant.now();

        public String getId() {
            return id;
        }

        public Instant getTimestamp() {
            return timestamp;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试过如下配置:

@Configuration
@EnableWebFlux
public class WebConfig implements WebFluxConfigurer {
    //moved from application class
    @Bean …
Run Code Online (Sandbox Code Playgroud)

java spring spring-webflux

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

在Java中,添加else语句(在已经使用的if之后)是否需要更长的时间来执行?

既然已经评估了if语句,添加else语句是否会对性能产生影响?

一个实际的例子:

if(updateTime >= updateIncrement)
{
    update();
    updateTime = 0;
}
else
    updateTime+=deltaTime;
Run Code Online (Sandbox Code Playgroud)

VS:

if(updateTime >= updateIncrement)
{
    update();
    updateTime = -deltaTime;
}
updateTime+=deltaTime;
Run Code Online (Sandbox Code Playgroud)

编辑:为了澄清,我不是在寻找微观优化,这只是一个假设的"如何工作"的问题

java performance if-statement

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

在 Spring 数据的 RepositoryRestResource 中急切地加载 MongoDB @DBRef

我正在尝试使用RepositoryRestResource和实现一个rest apiRestTemplate

除了加载@DBRef 之外,一切都运行良好

考虑这个数据模型:

public class Order
{
   @Id
   String id;

   @DBRef
   Customer customer;

   ... other stuff
}

public class Customer
{
    @Id
    String id;

    String name;

    ...
}
Run Code Online (Sandbox Code Playgroud)

以及以下存储库(客户的类似存储库)

@RepositoryRestResource(excerptProjection = OrderSummary.class)
public interface OrderRestRepository extends MongoRepositor<Order,String>{}
Run Code Online (Sandbox Code Playgroud)

其余 api 返回以下 JSON:

{
  "id" : 4,
  **other stuff**,
  "_links" : {
    "self" : {
      "href" : "http://localhost:12345/api/orders/4"
    },
    "customer" : {
      "href" : "http://localhost:12345/api/orders/4/customer"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

如果由 resttemplate 正确加载,将创建一个新的 Order 实例,其中 customer = null

是否可以在存储库端急切地解决客户并嵌入 …

mongodb spring-data-rest spring-data-mongodb spring-boot

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

如何在 Rest 模板中重用用户 (authorization_code) 的 oauth2 令牌

我有3个应用程序

  1. 前端应用程序
  2. OAuth2认证服务器
  3. REST API(RepositoryRestResources)

我的用户必须先登录才能使用前端应用程序。这是通过 SSO 实现的。他们会收到一个令牌,该令牌在被允许进入之前由客户验证。

我想重用这个令牌来发出 api 请求。我的 REST api 应用程序使用相同的 SSO 登录(它是前端客户端的资源)进行保护,但我不知道如何“添加授权标头”以在我用于 api 请求的 RestTemplate 中使用。

我像这样创建我的restTemplate:

public static RestTemplate build()
    {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.registerModule(new Jackson2HalModule());
        mapper.registerModule(new JavaTimeModule());
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setSupportedMediaTypes(MediaType.parseMediaTypes("application/hal+json"));
        converter.setObjectMapper(mapper);
        return new RestTemplate(Arrays.asList(converter));
    }
Run Code Online (Sandbox Code Playgroud)

我的资源服务器配置:

@Configuration
@EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter
{

    @Value("${resource.id}")
    private String resourceId;

    @Override
    public void configure(HttpSecurity http) throws Exception
    {
        http
                .authorizeRequests()
                .antMatchers(HttpMethod.OPTIONS).permitAll()
                .anyRequest().authenticated()
                .and().exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler());
    }

    @Override
    public void configure(ResourceServerSecurityConfigurer …
Run Code Online (Sandbox Code Playgroud)

spring spring-security spring-data-rest spring-boot spring-security-oauth2

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