小编Nep*_*s76的帖子

我可以在Spring Rest方法中混合媒体类型吗?

我目前正在尝试编写一个接受文件上传的ReST方法.当用户提交文件时,我还希望他们添加描述以及关于文件内容的一些其他元数据(例如,与文件内容相关联的"类型").我正在使用Spring 4的Spring MVC Controller.

这是我想要做的一个例子:

@RequestMapping(value = "/file", method = RequestMethod.POST)
public @ResponseBody
ResponseEntity<MyFileDTO> uploadCustomAnnotationFile(
        @RequestParam("file") MultipartFile uploadFile,
        @RequestBody MyFileDetailsDTO fileDetails) {
    log.debug("This is working!");
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我尝试通过Swagger UI调用此方法,则会得到不受支持的媒体类型异常:

org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'multipart/form-data;boundary=----WebKitFormBoundarycAbgNBTQ09GQTBif' not supported
Run Code Online (Sandbox Code Playgroud)

我怀疑我不能在1个请求中混合application/json和multipart/form-data?

更新:基于zeroflagL的响应并遵循提供的链接到特定于我正在尝试做的文档,并使用@RequestPart而不是@RequestBody,我取得了很小的进展,但这仍然不是工作.

新方法签名:

@RequestMapping(value = "/file", method = RequestMethod.POST)
public @ResponseBody
ResponseEntity<MyFileDTO> uploadCustomAnnotationFile(
        @RequestPart MultipartFile uploadFile,
        @RequestPart MyFileDetailsDTO fileDetails) {
    log.debug("This is working!");
}
Run Code Online (Sandbox Code Playgroud)

新例外:

2014-12-11 09:21:45.237 [http-nio-8443-exec-8] ERROR c.i.h.c.ControllerExceptionHandler   [ControllerExceptionHandler.groovy:58] - Controller Exception
org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'fileDetails' is not present.
Run Code Online (Sandbox Code Playgroud)

托尼亚,提前谢谢

java rest spring spring-mvc

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

Spring 4 AOP @Aspect没有触发@RestController

我创建了一个Aspect,它执行基本的id比较,以确保用户属于创建所请求实体的同一组.我已成功将我的方面附加到@Service方法,但它在服务层上没有意义,我需要将它附加到@RestController方法.当我尝试这样做时,一切似乎都很好,但我的Aspect从未触发,日志也是静默的.

的pom.xml

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>4.1.7.RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

春天的背景

<context:annotation-config/>
<context:component-scan base-package="my.pkg"/>
<aop:aspectj-autoproxy/>
<aop:config proxy-target-class="true"/>
Run Code Online (Sandbox Code Playgroud)

方面

@Aspect
@Component
public class MyAspect {
    @Pointcut("within(@org.springframework.stereotype.Controller *)")
    public void controller() {}

@Pointcut("within(@org.springframework.web.bind.annotation.RestController *)")
    public void restController() {}

    @Pointcut("args(java.security.Principal,..)")
    public void principalArgPointcut() {}

    @Around("(controller() || restController()) && principalArgPointcut()")
    public Object validate(ProceedingJoinPoint point) throws Throwable {
        doValidationBefore();
        Object result = point.proceed();
        doValidationAfter();

        return result;
    }
}
Run Code Online (Sandbox Code Playgroud)

其中"doValidationBefore()"和"doValidationAfter()"将在验证失败时抛出异常.

最后,我的RestController

@RestController
@RequestMapping("/my-path")
public class MyController {
    @RequestMapping(value = "/{entityId}", method = RequestMethod.GET)
    public @ResponseBody
    ResponseEntity<MyEntity> getEntityDetails(Principal principal, @PathVariable("entityId") …
Run Code Online (Sandbox Code Playgroud)

spring-mvc spring-aop spring-4 spring-restcontroller

9
推荐指数
1
解决办法
6340
查看次数

使用 spring-boot 2.0.2.RELEASE 并且 FlyWay 未运行。没有错误

我有一个非常简单的设置,全新的项目,并且由于某种原因 Flyway 从不初始化。没有错误。我花了几个小时试图弄清楚我做错了什么,但无济于事。

此外,我使用 Spring Boot 1.4.2.RELEASE 对另一个项目进行了几乎相同的设置。唯一的区别是 application.yml 中的 Flyway 配置现在在“spring”上下文下,而不是在根目录下。

如果有人能看到我遗漏了什么,或错别字或其他什么,我将不胜感激。

我还尝试注释掉 flyway 特定的数据源配置,以便它只使用 spring 数据源,但这没有区别。

谢谢,托尼亚

pom.xml(相关部分):

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.2.RELEASE</version>
</parent>

<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-core</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

application.yml(相关部分)

spring.profiles: default

spring:
    flyway:
        url: jdbc:mysql://localhost:3306/test_schema?tinyInt1isBit=false
        user: adminuser
        password: adminuser

    datasource:
        url: jdbc:mysql://localhost:3306/test_schema?tinyInt1isBit=false
        username: restricteduser
        password: restricteduser
        driver-class-name: com.mysql.jdbc.Driver

    jpa:
        database: MYSQL
        hibernate.ddl-auto: none
        show-sql: true
        properties.hibernate.dialect: org.hibernate.dialect.MySQL5InnoDBDialect
Run Code Online (Sandbox Code Playgroud)

数据库设置脚本:

CREATE SCHEMA `test_schema`;

CREATE USER IF NOT EXISTS 'restricteduser'@'%' IDENTIFIED BY 'restricteduser';
GRANT SELECT,INSERT,UPDATE,DELETE ON test_schema.* TO 'restricteduser'@'%' WITH GRANT OPTION; …
Run Code Online (Sandbox Code Playgroud)

flyway spring-boot

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

缺少 JaCoCo 覆盖范围

我已经为此工作了几个小时,我在网上找到的每一个我尝试过的解决方案都没有任何区别。

我有一个用 Kotlin 编写的项目。我的声纳服务器上安装了 Kotlin 插件。我正在使用 JaCoCo 插件为声纳生成代码覆盖率报告。

我的项目是多模块的,但只有 1 个子模块有单元测试,所以我在那里定义了我的 JaCoCo 插件。

我也在使用 Surefire 插件。

这是我在 pom.xml 中的属性

<properties>
    <sonar.java.binaries>target/classes</sonar.java.binaries>
    <sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
    <sonar.jacoco.reportPath>target/jacoco.exec</sonar.jacoco.reportPath>
    <sonar.language>kotlin</sonar.language>
    <sonar.sources>src/main</sonar.sources>
    <sonar.tests>src/test</sonar.tests>
    <sonar.verbose>true</sonar.verbose>
    <sonar.jacoco.reportsPath>target</sonar.jacoco.reportsPath>
    <sonar.junit.reportsPath>target/surefire-reports</sonar.junit.reportsPath>
    <sonar.surefire.reportsPath>target/surefire-reports</sonar.surefire.reportsPath>

    <surefireArgLine/>
</properties>
Run Code Online (Sandbox Code Playgroud)

这是我的万无一失的插件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <excludes>
            <exclude>**/*IT.java</exclude>
        </excludes>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-surefire-provider</artifactId>
            <version>1.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit-jupiter.version}</version>
        </dependency>
    </dependencies>
</plugin>
Run Code Online (Sandbox Code Playgroud)

这是我的 JaCoCo 插件:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.1</version>
    <configuration>
        <append>true</append>
    </configuration>
    <executions>
        <execution>
            <id>pre-unit-test</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <destFile>target/jacoco.exec</destFile>
                <propertyName>surefireArgLine</propertyName>
            </configuration>
        </execution>
        <execution>
            <id>post-unit-test</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal> …
Run Code Online (Sandbox Code Playgroud)

maven maven-surefire-plugin jacoco sonarqube jacoco-maven-plugin

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