项目设置:Gradle5、Spring5
我们想使用JUnit5作为我们的测试工具。我们从Spring5依赖项中排除了JUnit4,但我们有一个仍然依赖于JUnit4 的第 3 方库,并且不允许排除它。我们尝试全局排除JUnit4,但我们的项目无法编译。
目标:我们的目标是隐藏JUnit4的编译依赖项,以便开发人员不会意外使用JUnit4类而不是JUnit5类。
摇篮:
dependencies {
implementation('org.springframework.boot:spring-boot-starter')
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'junit', module: 'junit'
}
testImplementation('org.testcontainers:testcontainers:1.10.2') {
exclude group: 'junit', module: 'junit'
}
testImplementation('org.testcontainers:postgresql:1.7.3') {
exclude group: 'junit', module: 'junit'
}
testImplementation('org.testcontainers:junit-jupiter:1.11.2')
testImplementation('org.mockito:mockito-junit-jupiter:2.23.0')
testImplementation('org.junit.jupiter:junit-jupiter-api:5.3.1')
testRuntimeOnly('org.junit.jupiter:junit-jupiter-engine:5.3.1')
}
Run Code Online (Sandbox Code Playgroud)
我们的依赖项中的相关部分:
testCompileClasspath
+--- org.springframework.boot:spring-boot-starter-test -> 2.1.3.RELEASE
... (junit4 excluded)
+--- org.testcontainers:testcontainers:1.10.2
| +--- junit:junit:4.12
| | \--- org.hamcrest:hamcrest-core:1.3
...
+--- org.junit.jupiter:junit-jupiter-api:5.3.1 (*)
\--- org.junit.jupiter:junit-jupiter-engine:5.3.1 …Run Code Online (Sandbox Code Playgroud) 我必须创建一个类的层次结构,其中超类具有@CollectionTable表示地图的类.我试图实现它,但它只适用于一个子类.
稳定(工作)代码如下所示:
@MappedSuperclass
public class Animal {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
}
@Entity(name = "cats")
@Audited
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.CHAR)
@DiscriminatorValue(value = Cat.PET_TYPE)
public abstract class Cat extends Animal {
public static final String PET_TYPE = "C";
@ElementCollection(fetch = FetchType.EAGER)
@MapKeyColumn(name = "name")
@Column(name = "value")
@CollectionTable(name = "cat_properties", joinColumns = @JoinColumn(name = "cat_id"))
private Map<String, String> properties = new …Run Code Online (Sandbox Code Playgroud) 我想在我的 Spring Boot 应用程序中创建一个自定义注释,它总是向我的类级别 RequestMapping path添加前缀。
我的控制器:
import com.sagemcom.smartvillage.smartvision.common.MyApi;
import org.springframework.web.bind.annotation.GetMapping;
@MyApi("/users")
public class UserController {
@GetMapping("/stackoverflow")
public String get() {
return "Best users";
}
}
Run Code Online (Sandbox Code Playgroud)
我的自定义注释
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RestController
@RequestMapping(path = "/api")
public @interface MyApi {
@AliasFor(annotation = RequestMapping.class)
String value();
}
Run Code Online (Sandbox Code Playgroud)
目标:最终的映射如下:/api/users/stackoverflow
笔记:
server.servlet.context-path不是一个选项,因为我想创建其中几个目标
我想向某个主题发送一条消息,稍后我将使用客户端应用程序处理该消息。为此,我使用 Spring Boot 和 Spring Integration Java DSL 及其 JMS 模块。作为消息代理,我使用本机 ActiveMQ Artemis。
这是我的设置
演示应用程序.java
@SpringBootApplication
public class DemoApplication {
private static final Logger logger = LoggerFactory.getLogger(DemoApplication.class);
public interface StarGate {
void sendHello(String helloText);
}
@Autowired
private ConnectionFactory connectionFactory;
@Bean
public IntegrationFlow mainFlow() {
return IntegrationFlows
.from(StarGate.class)
.handle(Jms.outboundAdapter(connectionFactory)
.configureJmsTemplate(jmsTemplateSpec -> jmsTemplateSpec
.deliveryPersistent(true)
.pubSubDomain(true)
.sessionTransacted(true)
.sessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE)
.explicitQosEnabled(true)
)
.destination(new ActiveMQTopic("wormhole")))
.get();
}
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
StarGate stargate = context.getBean(StarGate.class);
stargate.sendHello("Jaffa, kree!");
logger.info("Hello …Run Code Online (Sandbox Code Playgroud) jms spring-integration activemq-artemis spring-integration-dsl