我有三个 Maven 项目,一个是 pom 包,其他 2 个是 jar 包这里是 admin-aggregator pom
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sounds.bvs</groupId>
<artifactId>admin-aggregator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>../admin-lib</module>
<module>../admin-rest</module>
</modules>
<properties>
<project.build.SourceEncoding>UTF-8</project.build.SourceEncoding>
<spring-boot-version>2.0.4.RELEASE</spring-boot-version>
<spring-version>2.0.4.RELEASE</spring-version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<start-class>com.sounds.bvs.AdminRestApp</start-class>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.sounds.bvs</groupId>
<artifactId>admin-lib</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.sounds.bvs</groupId>
<artifactId>admin-rest</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
<mainClass>${start-class}</mainClass>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven.compiler.plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<showDeprication>true</showDeprication>
<showWarnings>true</showWarnings>
</configuration>
</plugin> …
Run Code Online (Sandbox Code Playgroud) 我试图了解runtime
maven中作用域依赖的特定需求.在哪种情况下我需要这个,哪个都没有compile
,provided
范围不会?
例如,compile
当我必须直接在代码中调用库API(针对它进行编译)并将依赖项打包到工件或依赖项目工件中时,我会使用scope作为依赖项.
provided
当我必须针对API进行编译时,我会使用范围,但不要打包它(除了它在运行时可用).
但我runtime
什么时候需要范围?这是针对什么情况,当我不直接调用库API(而是使用反射),但是想要将它打包在工件中?为什么不只是使用compile
范围?唯一的好处是编译时间更快,或者是否有其他特殊的runtime
范围无法通过compile
范围实现或避免?
我正在尝试使用 来测试我的 Rest Controller 的 Rest 合约@WebMvcTest
,例如:
@RunWith(SpringRunner.class)
@WebMvcTest(MyController.class)
class MyControllerTest {
Run Code Online (Sandbox Code Playgroud)
我正在测试的方法仅使用 MyFirstService,但在类内部还有另外两个服务(MySecondService 和 MyThirdService)由另外两个方法使用。
问题是使用@MockBean
. 测试类在决赛中似乎是这样的:
@RunWith(SpringRunner.class)
@WebMvcTest(MyController.class)
class MyControllerTest {
@MockBean
private MyFirstService s1; // internal method mocked for the test
@MockBean
private MySecondService s2; //never used
@MockBean
private MyThirdService s3; //never used
...
@Test
public void testMethod() [
// mock s1
// calls rest controller method
MvcResult mvcResult = mvc.perform(post("/products")
.header("Content-Type", "application/json")
.content(json))
.andExpect(status().isCreated())
.andReturn();
}
}
Run Code Online (Sandbox Code Playgroud)
我认为这个解决方案对于这些注释并不优雅......声明的 s2 和 s3 似乎没有被阅读代码的人使用。注入到 …
我正在尝试使用来自主类的自动装配引用并且面临:
无法对非静态字段 zipCodeLookupService 进行静态引用。
这是显而易见的。但我想知道如何处理这种情况。涉及主类时自动装配的正确方法是什么。我的代码如下 -
接口类
package com.example.services;
public interface IZipCodeLookup {
String retriveCityForZip(String zipCode);
}
Run Code Online (Sandbox Code Playgroud)
服务等级
package com.example.services;
import org.springframework.stereotype.Service;
@Service
public class ZipCodeLookupService implements IZipCodeLookup {
@Override
public String retriveCityForZip(String zipCode) {
//below is mock code. actual code does a db lookup using a DAO.
if(zipCode=="94123") return "San Francisco";
return "not found in DB";
}
}
Run Code Online (Sandbox Code Playgroud)
这是需要服务类的主类
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.example.services.IZipCodeLookup;
@SpringBootApplication
public class AutowireWithMainClassApplication {
@Autowired
IZipCodeLookup zipCodeLookupService;
public static …
Run Code Online (Sandbox Code Playgroud) spring dependency-injection inversion-of-control autowired spring-boot
代码约定说控制器中没有逻辑。所有这些都应在服务层中处理。我的问题尤其是关于返回ResponseEntity的问题。
应该在RestController还是在Service层中处理?
我尝试了两种方式。我认为RestController是返回ResponseEntity的合适位置。因为我们在RestController中使用映射。
另一方面,我们知道控制器不应包含任何逻辑。
@GetMapping("/{id}")
public ResponseEntity<Employee> getEmployee(@PathVariable Long id) {
return ResponseEntity.ok(employeeService.findEmployeeById(id);
}
Run Code Online (Sandbox Code Playgroud)
要么
@GetMapping("/{id}")
public ResponseEntity<Employee> getEmployee(@PathVariable Long id) {
return employeeService.findEmployeeById(id);
}
Run Code Online (Sandbox Code Playgroud)
我的另一个担心是用于异常处理的ControllerAdvice。最好使用哪种方式?
感谢您的进步。
标题可能会产生误导,但作为非本地人,我无法找到更好的标题.
假设我有两个课程,Dog
并且Fox
:
public class Dog {
public String bark() {
return "Wuff";
}
public String play(Dog d) {
return "Wuff" + d.bark();
}
}
public class Fox extends Dog {
public String bark() {
return "Ringding" ;
}
public String play(Fox f) {
return "Ringding" + f.bark();
}
}
Run Code Online (Sandbox Code Playgroud)
我创建了一些实例,并调用了一些方法
Fox foxi = new Fox();
Dog hybrid = new Fox();
System.out.println(hybrid.play(foxi)); // Output number 1
System.out.println(foxi.play(hybrid)); // Output number 2
Run Code Online (Sandbox Code Playgroud)
对于1. Ouput我的预期,"RingdingRingding"
因为hybrid
实际上是对一个实例 …
在我的项目中,我总是使用,<dependency><dependency>
但我可以<parent></parent>
在一些项目中看到pom.xml
:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
Run Code Online (Sandbox Code Playgroud)
所以我想知道什么时候使用它.
Spring Boot似乎有一个默认行为来处理某些异常.我有一个休息控制器.如果我没有HttpRequestMethodNotSupportedException
在带@ControllerAdvice
注释的rest控制器中处理,则应用程序返回包含错误消息的默认JSON响应.
我不想替换这个JSON响应,但我确实希望在发生时记录其他信息(例如记录某个请求者的IP地址).
有没有办法用带@ExceptionHandler
注释的方法或其他机制来做到这一点?
通过使用@MockitoExtension
from 的扩展mockito-junit-jupiter
,测试类构造函数中的模拟为 null。
使用的依赖项:
模拟-junit-jupiter:2.18.0
单元-jupiter-api:5.1.1
我们可以仅使用模拟字段来重现该问题,但测试对象构造函数对模拟的使用很好地说明了典型场景:
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
public class FooTest {
@Mock
Bar mockBar;
Foo foo;
public FooTest() {
foo = new Foo(mockBar); // mockBar is null here
}
@Test
public void doThis() {
Mockito.when(mockBar.doThat())
.thenReturn(Mockito.anyInt());
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
结果,模拟对象四处走动,但不与Foo
被测对象关联。
为什么 ?使用什么解决方法?
我可能会为遗留项目做一些交叉编译,我注意到最近的 JDK 仅限于某些特定版本的source
,target
和release
JVM 参数。
如何获得这些参数的支持版本?
java ×8
spring-boot ×5
spring ×4
maven ×3
autowired ×1
compilation ×1
dependencies ×1
inheritance ×1
jar ×1
javac ×1
junit5 ×1
mocking ×1
mockito ×1
overloading ×1
overriding ×1
spring-mvc ×1
spring-rest ×1
testing ×1