对于我的Spring-Boot应用程序,我通过@Configuration文件提供了RestTemplate,因此我可以添加合理的默认值(ex Timeouts).对于我的集成测试,我想模拟RestTemplate,因为我不想连接到外部服务 - 我知道期望的响应.我尝试在集成测试包中提供不同的实现,希望后者将覆盖实际的实现,但是反过来检查日志:真正的实现覆盖了测试.
如何确保TestConfig中的那个是使用的?
这是我的配置文件:
@Configuration
public class RestTemplateProvider {
private static final int DEFAULT_SERVICE_TIMEOUT = 5_000;
@Bean
public RestTemplate restTemplate(){
return new RestTemplate(buildClientConfigurationFactory());
}
private ClientHttpRequestFactory buildClientConfigurationFactory() {
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setReadTimeout(DEFAULT_SERVICE_TIMEOUT);
factory.setConnectTimeout(DEFAULT_SERVICE_TIMEOUT);
return factory;
}
}
Run Code Online (Sandbox Code Playgroud)
整合测试:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestConfiguration.class)
@WebAppConfiguration
@ActiveProfiles("it")
public abstract class IntegrationTest {}
Run Code Online (Sandbox Code Playgroud)
TestConfiguration类:
@Configuration
@Import({Application.class, MockRestTemplateConfiguration.class})
public class TestConfiguration {}
Run Code Online (Sandbox Code Playgroud)
最后是MockRestTemplateConfiguration
@Configuration
public class MockRestTemplateConfiguration {
@Bean
public RestTemplate restTemplate() {
return Mockito.mock(RestTemplate.class)
}
}
Run Code Online (Sandbox Code Playgroud) 我的应用程序加载了应该处理的实体列表.这发生在使用调度程序的类中
@Component
class TaskScheduler {
@Autowired
private TaskRepository taskRepository;
@Autowired
private HandlingService handlingService;
@Scheduled(fixedRate = 15000)
@Transactional
public void triggerTransactionStatusChangeHandling() {
taskRepository.findByStatus(Status.OPEN).stream()
.forEach(handlingService::handle);
}
}
Run Code Online (Sandbox Code Playgroud)
在我的HandlingService过程中,每个任务都在使用REQUIRES_NEW传播级别进行曝光.
@Component
class HandlingService {
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void handle(Task task) {
try {
processTask(task); // here the actual processing would take place
task.setStatus(Status.PROCCESED);
} catch (RuntimeException e) {
task.setStatus(Status.ERROR);
}
}
}
Run Code Online (Sandbox Code Playgroud)
代码的工作原理只是因为我在TaskScheduler类上启动了父事务.如果我删除@Transactional注释,则不再管理实体,并且对任务实体的更新不会传播到db.我发现使调度方法具有事务性是不自然的.
从我看到我有两个选择:
1.保持现在的代码.
2. @Transactional从Scheduler中删除注释,传递任务的id并在HandlingService中重新加载任务实体.
@Component
class HandlingService {
@Autowired …Run Code Online (Sandbox Code Playgroud) 我正在尝试设置一个Spring Cloud Config服务器,该服务器使用ssh私钥的自定义位置.我需要为密钥指定自定义位置的原因是因为运行应用程序的用户没有主目录..所以我没有办法使用~/.ssh我的密钥的默认目录.我知道可以选择创建只读帐户并在配置中提供用户/密码,但ssh方式接缝更干净.
有没有办法设置这个?
我正在构建一个家庭项目,我想创建一个用于建议地址的自动完整文本视图.为了使故事简短我想世界希望它就像谷歌地图中的那个.我熟悉java和webservices但我真的很好不知道从哪里开始.
谢谢.
我不确定为什么,但是它@ControllerAdvice会覆盖Exception使用@ResponseStatus注释在级别定义的响应代码.
例外:
@ResponseStatus(HttpStatus.BAD_REQUEST)
public class GreetException extends RuntimeException {}
Run Code Online (Sandbox Code Playgroud)
控制器:
@RestController
@RequestMapping("/")
public class GreetController {
@RequestMapping(method = RequestMethod.GET)
public String greet() {
throw new GreetException();
}
}
Run Code Online (Sandbox Code Playgroud)
控制建议:
@ControllerAdvice
public class ExceptionConfiguration {
@ResponseStatus(HttpStatus.CONFLICT)
@ExceptionHandler(RuntimeException.class)
public void handleConflict() {}
}
Run Code Online (Sandbox Code Playgroud)
来自GreetController被称为响应的greet方法的响应是409 - CONFLICT.因为我特意提供了异常级别的响应代码,我预计这将是返回的代码(400 - BAD_REQUEST).
当然,这是一个过于简化的示例,但我们定义了一个带有RuntimeException定义的控制器建议,因此我们可以为每个未捕获的异常分配一个id.
实现预期行为的正确方法是什么?
我正在使用Jersey 1.8,我所指的教程很安静.什么都行不通.我指的是这里给出的教程
并获得一个未找到的类异常.根据教程,我创建了Java类,并配置了我的web.xml.它向我展示了一个例外,我没有办法解决这个问题.我想有一个完整的泽西实施最新教程.如果有一些东西比泽西的REST实施更好,请建议.我已经开始使用基于REST的Web服务了,如果你可以建议我从哪里开始(我只对REST感兴趣),我将不胜感激.下面是我用eclipse编写和编译的代码.
Hello.java
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/hello")
public class Hello {
//This method prints the Plain Text
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello()
{
return "Hello Jersey";
}
//This is the XML request output
@GET
@Produces(MediaType.TEXT_XML)
public String sayXMLHello()
{
return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
}
//This result is produced if HTML is requested
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHTMLHello()
{
return "<html> " + "<title>" + "Hello Jersey" + …Run Code Online (Sandbox Code Playgroud) 我们正在开发一个应用程序,它使用部署在 AWS-ElasticBeanstalk 上的两种服务,比如说app1.beanstalk.com和app2.beanstalk.com。app1公开了一些内部 REST API ( app1.beanstalk.com/intenal/reports),我们要求使它们只能从 app2 访问。
我们很清楚,我们可以在应用程序级别阻止请求,但我们希望在此之前阻止......类似防火墙的东西。是否有任何与 Beanstalk 集成的 AWS 服务,并允许我们app1.beanstalk.com/intenal/*仅在请求来自某个安全组或子网 (VPC) 时才允许对特定 URL的请求
spring ×4
java ×3
spring-boot ×3
spring-cloud ×2
android ×1
hibernate ×1
java-ee ×1
jax-rs ×1
jersey ×1
jpa ×1
rest ×1
spring-data ×1
spring-mvc ×1
spring-test ×1
spring-web ×1