Install
openclaw skills install ah-java-enterpriseYou are a Java enterprise development expert specializing in Spring Boot, microservices architecture, and JVM optimization. Use when: java language mastery,...
openclaw skills install ah-java-enterpriseYou are a Java enterprise development expert specializing in Spring Boot, microservices architecture, and JVM optimization.
📎 Code example 1 (java) — see references/examples.md
// Configuration class
@Configuration
@PropertySource("classpath:application.yml")
public class AppConfig {
@Bean
@ConditionalOnProperty(name = "cache.enabled", havingValue = "true")
public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(10))
.serializeKeysWith(RedisSerializationContext.SerializationPair
.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair
.fromSerializer(new GenericJackson2JsonRedisSerializer()));
return RedisCacheManager.builder(connectionFactory)
.cacheDefaults(config)
.build();
}
@Bean
@Profile("!test")
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder
.setConnectTimeout(Duration.ofSeconds(5))
.setReadTimeout(Duration.ofSeconds(10))
.interceptors(new LoggingInterceptor())
.build();
}
}
// Circuit breaker with Resilience4j
@Component
public class ExternalServiceClient {
private final RestTemplate restTemplate;
private final CircuitBreaker circuitBreaker;
public ExternalServiceClient(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
this.circuitBreaker = CircuitBreaker.ofDefaults("external-service");
circuitBreaker.getEventPublisher()
.onStateTransition(event ->
log.info("Circuit breaker state transition: {}", event));
}
@Retry(name = "external-service", fallbackMethod = "fallbackResponse")
@CircuitBreaker(name = "external-service", fallbackMethod = "fallbackResponse")
@Bulkhead(name = "external-service")
public ExternalData fetchData(String id) {
return Decorators.ofSupplier(() ->
restTemplate.getForObject("/api/data/" + id, ExternalData.class))
.withCircuitBreaker(circuitBreaker)
.withRetry(Retry.ofDefaults("external-service"))
.decorate()
.get();
}
public ExternalData fallbackResponse(String id, Exception ex) {
log.warn("Fallback triggered for id: {}", id, ex);
return ExternalData.empty();
}
}
// Repository with custom queries
@Repository
public interface UserRepository extends JpaRepository<User, UUID>,
JpaSpecificationExecutor<User> {
@Query("SELECT u FROM User u WHERE u.email = :email AND u.active = true")
Optional<User> findActiveByEmail(@Param("email") String email);
@Modifying
@Query("UPDATE User u SET u.lastLogin = :timestamp WHERE u.id = :id")
void updateLastLogin(@Param("id") UUID id, @Param("timestamp") Instant timestamp);
@EntityGraph(attributePaths = {"roles", "permissions"})
Optional<User> findWithRolesById(UUID id);
// Dynamic queries with Specifications
default Page<User> findWithFilters(UserFilter filter, Pageable pageable) {
Specification<User> spec = Specification.where(null);
if (filter.getName() != null) {
spec = spec.and((root, query, cb) ->
cb.like(cb.lower(root.get("name")),
"%" + filter.getName().toLowerCase() + "%"));
}
if (filter.getCreatedAfter() != null) {
spec = spec.and((root, query, cb) ->
cb.greaterThanOrEqualTo(root.get("createdAt"),
filter.getCreatedAfter()));
}
return findAll(spec, pageable);
}
}
// WebFlux reactive controller
@RestController
@RequestMapping("/api/v1/stream")
public class StreamController {
private final ReactiveUserService userService;
@GetMapping(value = "/users", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<ServerSentEvent<UserEvent>> streamUsers() {
return userService.getUserEvents()
.map(event -> ServerSentEvent.<UserEvent>builder()
.id(event.getId())
.event(event.getType())
.data(event)
.retry(Duration.ofSeconds(5))
.build())
.doOnError(error -> log.error("Error in stream", error))
.onErrorResume(error -> Flux.empty());
}
@PostMapping("/process")
public Mono<ProcessResult> processAsync(@RequestBody Flux<DataChunk> chunks) {
return chunks
.buffer(100)
.flatMap(batch -> processBatch(batch))
.reduce(ProcessResult::merge)
.timeout(Duration.ofMinutes(5))
.doOnSuccess(result -> log.info("Processing complete: {}", result));
}
}
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http
.csrf().disable()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeHttpRequests()
.requestMatchers("/api/public/**").permitAll()
.requestMatchers("/api/admin/**").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.oauth2ResourceServer()
.jwt()
.jwtAuthenticationConverter(jwtAuthenticationConverter())
.and()
.exceptionHandling()
.authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))
.and()
.build();
}
@Bean
public JwtAuthenticationConverter jwtAuthenticationConverter() {
JwtGrantedAuthoritiesConverter authoritiesConverter =
new JwtGrantedAuthoritiesConverter();
authoritiesConverter.setAuthorityPrefix("ROLE_");
authoritiesConverter.setAuthoritiesClaimName("roles");
JwtAuthenticationConverter converter = new JwtAuthenticationConverter();
converter.setJwtGrantedAuthoritiesConverter(authoritiesConverter);
return converter;
}
}
// Integration testing
@SpringBootTest
@AutoConfigureMockMvc
@TestPropertySource(locations = "classpath:application-test.yml")
class UserControllerIntegrationTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private UserService userService;
@Test
@WithMockUser(roles = "ADMIN")
void shouldCreateUser() throws Exception {
CreateUserRequest request = new CreateUserRequest("John", "john@example.com");
UserDto response = new UserDto(UUID.randomUUID(), "John", "john@example.com");
when(userService.create(any())).thenReturn(response);
mockMvc.perform(post("/api/v1/users")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.name").value("John"))
.andExpect(jsonPath("$.email").value("john@example.com"));
}
@Test
@Sql("/test-data/users.sql")
@Transactional
@Rollback
void shouldFindUsersByFilter() {
// Test with actual database
}
}
📎 Code example 2 (java) — see references/examples.md
When implementing Java solutions:
Always prioritize:
For detailed code examples and implementation patterns, see references/examples.md.