我有一个模型类
public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; }
public ICollection<SelectListItem> CourseList { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
和
public class StudentContext : DbContext
{
public DbSet<Student> Students { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我试着用它作为
List<Student> sList = db.Students.ToList();
Run Code Online (Sandbox Code Playgroud)
我得到了以下错误
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'SelectListItem' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'SelectListItems' is based on type 'SelectListItem' that has no keys defined.
Run Code Online (Sandbox Code Playgroud)
请告诉我在哪里做错了.
model-view-controller asp.net-mvc asp.net-mvc-3 asp.net-mvc-4
我有一个 Spring Boot 2 应用程序,其中静态资源是:
src
|- main
|-resources
|-static
|-js/myjs.js
|-style
|-css/mycss.css
Run Code Online (Sandbox Code Playgroud)
在我的模板文件中:
<link rel="stylesheet" type="text/css" href="/style/css/mycss.css">
<script src="/js/myjs.js"></script>
Run Code Online (Sandbox Code Playgroud)
这工作正常。
不过我想启用浏览器缓存和 gzip 传输。为此,我创建了以下 WebConfig:
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/static/**")
.addResourceLocations("/static/")
.setCachePeriod(3600)
.resourceChain(true)
.addResolver(new GzipResourceResolver())
.addResolver(new PathResourceResolver());
}
}
Run Code Online (Sandbox Code Playgroud)
该应用程序仍然可以运行,但不会缓存或压缩静态内容:
知道我做错了什么吗?
我已经尝试了所有3个解决方案,建议用什么方法来处理spring-webflux中的错误,但是WebExceptionHandler没有被调用.我在用Spring Boot 2.0.0.M7.Github回复这里
@Configuration
class RoutesConfiguration {
@Autowired
private lateinit var testService: TestService
@Autowired
private lateinit var globalErrorHandler: GlobalErrorHandler
@Bean
fun routerFunction():
RouterFunction<ServerResponse> = router {
("/test").nest {
GET("/") {
ServerResponse.ok().body(testService.test())
}
}
}
}
@Component
class GlobalErrorHandler() : WebExceptionHandler {
companion object {
private val log = LoggerFactory.getLogger(GlobalErrorHandler::class.java)
}
override fun handle(exchange: ServerWebExchange?, ex: Throwable?): Mono<Void> {
log.info("inside handle")
/* Handle different exceptions here */
when(ex!!) {
is ClientException -> exchange!!.response.statusCode = …Run Code Online (Sandbox Code Playgroud) exception-handling reactive-programming spring-boot spring-webflux
我正在尝试将在 java 8 上运行的 spring boot 2.3 应用程序迁移到 spring boot 3 java 17。问题出在存储库中。
其查询方法如下:
@Query(value = "SELECT ca FROM CorporateActionsV2 ca WHERE ( ca.dividendAmount, TO_CHAR(TO_DATE(ca.effectiveDate) - 5,'IYYY'), TO_CHAR(TO_DATE(ca.effectiveDate) - 5,'IW'), ca.tickerSerial) IN (SELECT c.dividendAmount, TO_CHAR(TO_DATE(c.effectiveDate) - 5,'IYYY'), TO_CHAR(TO_DATE(c.effectiveDate) - 5,'IW'), c.tickerSerial FROM CorporateActionsV2 c WHERE c.dividendAmount IS NOT NULL AND c.eventLabel = 'Dividend' AND c.actionStatus != 'Deleted'" +
" GROUP BY c.dividendAmount, TO_CHAR(TO_DATE(c.effectiveDate) - 5,'IYYY'), TO_CHAR(TO_DATE(c.effectiveDate) - 5,'IW'), c.tickerSerial HAVING COUNT(*) > 1 )")
List<CorporateActionsV2> getDuplicateActions();
Run Code Online (Sandbox Code Playgroud)
该实体CorporateActionsV2具有以下日期结构:
@Temporal(TemporalType.DATE) …Run Code Online (Sandbox Code Playgroud) 今天我将我的 Webflux REST API 演示应用程序从 springboot 升级2.7.x到了 version 3.0.0。在使用 SpringSecurity 测试 POST 调用时,我收到了403 Forbidden消息An expected CSRF token cannot be found。我仔细检查了我的安全配置,没有发现任何问题。
@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
return http
.csrf().disable()
.authorizeExchange()
.pathMatchers(HttpMethod.GET, "/actuator/**").permitAll()
.pathMatchers(HttpMethod.POST, "/api/v1/users", "/api/v1/users/**").hasRole(ReactiveConstant.SECURITY_ROLE_ADMIN) // Only admin can do POST
.pathMatchers(HttpMethod.GET, "/api/v1/users", "/api/v1/users/**").hasAnyRole(ReactiveConstant.SECURITY_ROLE_USER, ReactiveConstant.SECURITY_ROLE_ADMIN) // user can only do GET
.anyExchange().authenticated()
.and().formLogin()
.and().httpBasic()
.and().formLogin().disable()
.build();
}
Run Code Online (Sandbox Code Playgroud)
这适用于 SpringBoot 2.7.5 版本。我的build.gradle文件,
plugins {
id 'org.springframework.boot' version '3.0.0'
id 'io.spring.dependency-management' version '1.1.0' …Run Code Online (Sandbox Code Playgroud) 我有一个Spring boot 1.4.3项目.最近我提出了一个要求,我必须将日志从服务器发送到我的Web应用程序并在网页上打印日志.我知道WebSockets,但我正在寻找更好的解决方案,我遇到了,Reactive Programming和gRPC.
Spring在Spring版本5中支持Reactive Programming,但我对gRPC和Reactive Programming非常困惑.gRPC具有双向流,它建立在Netty之上,提供与从服务器向客户端推送数据(如Reactive Programming)相同的功能.那么我应该使用哪一个,如果你能在这种混乱中清除我,那将是非常好的.
此外,如果我转移到支持Spring Version 5的Spring Boot 2,该项目将在Netty上运行.我的困惑是,我是否必须在不同的容器上运行我的应用程序,例如Jetty服务器上的普通REST端点和netty服务器上的Reactive API,或者Spring将通过处理netty上的响应请求和剩余的一般REST来为我开箱即用Jetty服务器上的API,因为据我所知Netty不是Servlet容器.
我有清单List<Mono<String>>.每个Mono代表API调用,我在I/O上等待结果.问题是有些调用没有返回任何内容(空字符串),我需要在这种情况下再次重复它们.
现在它看起来像这样:
val firstAskForItemsRetrieved = firstAskForItems.map {
it["statistic"] = (it["statistic"] as Mono<Map<Any, Any>>).block()
it
}
Run Code Online (Sandbox Code Playgroud)
我正在等待所有Monos完成,然后在空身的情况下我重复请求
val secondAskForItem = firstAskForItemsRetrieved
.map {
if ((it["statistic"] as Map<Any, Any>).isEmpty()) {
// repeat request
it["statistic"] = getUserItem(userName) // return Mono
} else
it["statistic"] = Mono.just(it["statistic"])
it
}
Run Code Online (Sandbox Code Playgroud)
然后再次阻止每个项目
val secondAskForItemsRetrieved = secondAskForItems.map {
it["statistic"] = (it["statistic"] as Mono<Map<Any, Any>>).block()
it
}
Run Code Online (Sandbox Code Playgroud)
我觉得这看起来很难看
有没有其他方法可以在Mono中重试呼叫,如果它失败了,而不是手动操作?
它是否阻止每个项目以正确的方式获取它们?
如何使代码更好?
谢谢.
我一直在使用Spring Boot 2.0.1及其Webflux库来研究示例反应式Web api。我一直在网上查看示例以尝试构建它,但是我在两件事上感到困惑。以下是我有两个问题。
1)如何返回响应实体的流量,当我尝试时出现错误,提示只能返回单个响应实体。下面是我当前的代码。
@Service
public class MovieServiceImpl implements MovieService {
@Autowired
private MovieRepository movieRepository;
@Override
public Flux<Movie> list(){
return movieRepository.findAll();
}
}
@RestController
public class MovieRestController {
@Autowired
private MovieService movieService;
@GetMapping(value = "/movies")
public Flux<Movie> list() {
return movieService.list();
}
}
Run Code Online (Sandbox Code Playgroud)
2)更新对象时,我使用flatMap更新保存在Mongo中的对象,然后使用Map将其转换为响应实体。我的问题是为什么我在这里使用flatMap而不是map?我从在线示例中获取了此代码,但是没有示例说明了flatMap的用法。我想了解为什么在这里使用它。下面是代码。
@Service
public class MovieServiceImpl implements MovieService {
@Autowired
private MovieRepository movieRepository;
@Override
public Mono<Movie> update(String id, MovieRequest movieRequest) {
return movieRepository.findById(id).flatMap(existingMovie -> {
if(movieRequest.getDescription() != null){
existingMovie.setDescription(movieRequest.getDescription());
}
if(movieRequest.getRating() != null){
existingMovie.setRating(movieRequest.getRating());
}
if(movieRequest.getTitle() != …Run Code Online (Sandbox Code Playgroud)