这是Java 8流中要处理的Java对象
private static List<Person> getPersons() {
List<Person> results = new ArrayList<Person>();
results.add(new Person("Elsdon", "Jaycob", "Java programmer", "male", 43, 2000));
results.add(new Person("Tamsen", "Brittany", "Java programmer", "female", 23, 1500));
results.add(new Person("Floyd", "Donny", "Java programmer", "male", 33, 1800));
results.add(new Person("Sindy", "Jonie", "Java programmer", "female", 32, 1600));
results.add(new Person("Vere", "Hervey", "Java programmer", "male", 22, 1200));
results.add(new Person("Maude", "Jaimie", "Java programmer", "female", 27, 1900));
results.add(new Person("Shawn", "Randall", "Java programmer", "male", 30, 2300));
results.add(new Person("Jayden", "Corrina", "Java programmer", "female", 35, 1700));
results.add(new Person("Palmer", "Dene", "Java …Run Code Online (Sandbox Code Playgroud) 这些是我的简单 Thymeleaf 表 HTML 文件和 Spring MVC 控制器代码。下面首先是我的表格图像。
我尝试制作一些 html 代码,以便在单击“编辑”或“删除”链接时将帖子 id 值传输到查看代码,但我不知道该怎么做。这些是我的 Spring MVC 控制器代码和 view.html 代码。
@Controller
public class PostController {
@Autowired
private PostService postService;
@RequestMapping("/posts/view/{id}")
public String view(@PathVariable("id") Long id, Model model) {
Post post = postService.findById(id);
model.addAttribute("post", post);
return "posts/view";
}
Run Code Online (Sandbox Code Playgroud)
和,
<table id="blogTable" border="1" width ="1000" height="400" align = "center">
<thead>
<tr>
<th>Post ID</th>
<th>Post Title</th>
<th>Post Content</th>
<th>Date</th>
<th>Author</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr th:each="post : ${posts}">
<td th:text="${post.id}">Post ID</td>
<td th:text="${post.title}">Post Title</td>
<td …Run Code Online (Sandbox Code Playgroud) 我尝试制作 spring boot webflux 的处理程序和路由器类。模型类是用户类。代码是
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Document(collection="Users")
public class User {
@Id
private String _id;
@Indexed(unique = true)
private Long id;
@Indexed(unique=true)
private String username;
private String password;
private String email;
private String fullname;
private String role;
}
Run Code Online (Sandbox Code Playgroud)
下面是 webflux 项目的处理程序类。在register方法中,我制作了id重复测试代码。但这是完全错误的。
@Component
public class UserHandler {
@Autowired
private UserReactiveMongoRepository userRepository;
public Mono<ServerResponse> register(ServerRequest request) {
Mono<User> monoUser = request.bodyToMono(User.class);
String id = monoUser.map(u -> u.get_id()).toString();
if(userRepository.existsById(id) == null)
return ServerResponse.ok().build(userRepository.insert(monoUser));
return ServerResponse.ok().build();
}
}
Run Code Online (Sandbox Code Playgroud)
我想从 spring webflux …
我正在努力处理引用对象上的 Spring Boot MongoDB 级联操作。以下是 MongoDB 文档架构类。
== 发帖
@Getter
@Setter
@Document(collection="Post") // (1)
public class Post {
@Id
private String _id;
@Indexed(unique = true)
private Long id;
private String title;
private String body;
private Date createdDate;
@DBRef(db = "User", lazy = true)
private User user;
@DBRef(db = "Tag", lazy = true)
private Collection<Tag> tags;
Run Code Online (Sandbox Code Playgroud)
== 用户
@Getter
@Setter
@Document(collection="User") // (1)
public class User {
@Id //(2)
private String _id;
@Indexed(unique = true)
private Long id;
@Indexed(unique=true) // (3) …Run Code Online (Sandbox Code Playgroud) 我尝试使用路由器和处理程序类制作 spring webflux 安全应用程序。首先,下面的代码是webflux安全的配置代码。
@Configuration
@EnableWebFluxSecurity
public class BlogWebFluxSecurityConfig {
@Bean
public MapReactiveUserDetailsService userDetailsService() {
UserDetails userWebFlux = User.withUsername("joseph").password("password").roles("USER").build();
return new MapReactiveUserDetailsService(userWebFlux);
}
@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http
.authorizeExchange()
.pathMatchers("/route/user/all", "/route/post/all").permitAll()
.pathMatchers(HttpMethod.GET, "/route/user/**", "/route/post/**").hasRole("USER")
.anyExchange().authenticated()
.and()
.httpBasic();
return http.build();
}
}
Run Code Online (Sandbox Code Playgroud)
接下来的代码是关于路由器类的。
@Configuration
@EnableWebFlux
public class BlogWebFluxEndpointRouter {
@Bean
public RouterFunction<ServerResponse> routesUser(UserHandler handler) {
return RouterFunctions.route(RequestPredicates.GET("/route/user/all"), handler::findAll)
.andRoute(RequestPredicates.GET("/route/user/id/{id}"), handler::findById)
.andRoute(RequestPredicates.GET("/route/user/username/{username}"), handler::findByUsername)
.andRoute(RequestPredicates.GET("/route/user/email/{email}"), handler::findByEmail)
.andRoute(RequestPredicates.POST("/route/user/create"), handler::register)
.andRoute(RequestPredicates.GET("/route/user/login/{username}/{password}"), handler::authenticate);
}
@Bean
public RouterFunction<ServerResponse> routesPost(PostHandler handler) {
return RouterFunctions.route(RequestPredicates.GET("/route/post/all"), handler::findAll)
.andRoute(RequestPredicates.GET("/route/post/id/{id}"), …Run Code Online (Sandbox Code Playgroud) 我在 Windows 10 上使用 docker desktop 和 minikube。我使用minikube docker-env如下命令找到了本地 docker 存储库的 IP 地址,
> minikube docker-env
SET DOCKER_TLS_VERIFY=1
SET DOCKER_HOST=tcp://172.17.105.232:2376
SET DOCKER_CERT_PATH=C:\Users\joseph\.minikube\certs
SET MINIKUBE_ACTIVE_DOCKERD=minikube
REM To point your shell to minikube's docker-daemon, run:
REM @FOR /f "tokens=*" %i IN ('minikube -p minikube docker-env') DO @%i
Run Code Online (Sandbox Code Playgroud)
并且我将 docker daemon 的 ip 地址设置为上述DOCKER_HOST值,而不是localhost我可以使用本地构建的 docker 图像而不会出错。但是在 minikube 仪表板的情况下,当我输入minikube dashboard命令时,IP 地址总是 localhost(127.0.0.1) 。所以我无法生成 kubernetes 命名空间和持久卷。它抛出错误
服务器找不到请求的资源
我认为这个问题是不同IP地址的授权问题。如何在 minukube 仪表板上配置静态或特定 ip 地址和端口号,以便我可以在 minikube 仪表板上生成命名空间和持久卷而不会出现此类错误?
我的源日期类型是EEE MMM dd HH:mm:ss zzz yyyy. 例如,Sat Dec 12 00:00:00 KST 2020但我的目标日期类型是格式为 Java 日期类型yyyy-MM-dd。所以我制作了将Java String类型转换EEE MMM dd HH:mm:ss zzz yyyy为java util对象的方法。
private static Date getDate(String beforeDate) throws Exception{
SimpleDateFormat readFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
Date rdate = readFormat.parse(beforeDate);
SimpleDateFormat writeFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
String format = writeFormat.format(rdate);
System.out.println(format);
return writeFormat.parse(format);
}
Run Code Online (Sandbox Code Playgroud)
System.out.println(format) line 打印出我期望的正确值。
2020-12-12
2020-12-12
2013-01-01
Run Code Online (Sandbox Code Playgroud)
但是返回值的类型是错误的。该方法的返回值似乎没有受到影响。
System.out.println(getDate("Sat Dec 12 00:00:00 KST 2020"));
Run Code Online (Sandbox Code Playgroud)
上一行Sat …
java ×3
spring-boot ×3
collectors ×1
date ×1
datetime ×1
docker ×1
java-stream ×1
json ×1
kubernetes ×1
minikube ×1
spring ×1
thymeleaf ×1