Tag: spring mvc

无法通过Springboot连接到远程MongoDB

我试图连接到一个远程的MongoDB。 由于我没有得到一个MongoOpenSocketException,我认为已经建立了一个连接,但需要更多的启动应用程序。 我错过了什么? Application.kt package hello import org.springframework.boot.SpringApplication import org.springframework.boot.autoconfigure.EnableAutoConfiguration import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration import org.springframework.context.annotation.Configuration /** * The main entry point to the application */ @EnableAutoConfiguration(exclude = arrayOf(MongoAutoConfiguration::class)) @Configuration class Application /** * Run the application * @param args The command line arguments */ fun main(args: Array) { SpringApplication.run(Application::class.java, *args) } 的build.gradle: // Top-level build file […]

春季启动ThymeLeaf和Kotlin – checkbox值没有被传递给控制器​​?

我正在尝试使用Spring Thymeleaf和Kotlin来提供HTML页面。 我希望它能够传递我按下“提交”时选择的HTML页面上的所有值,然后将所有提交内容存储到订单列表中。 class Order(val nameForTheOrder: String, val items: Map, val totalCost: BigDecimal) 控制器: @Controller class OrderController { @Autowired lateinit var orderService: OrderService @RequestMapping(“/orderHistory”) fun listOfOrders(model: Model) : String { model.addAttribute(“orderHistory”, orderService.getOrders()) return “orderHistory” } @RequestMapping(“/order/{nameForTheOrder}”) fun findOrder(@PathVariable(“nameForTheOrder”) orderId: String, model: Model) : String { model.addAttribute(“order”, orderService.findOrderbyId(orderId)) return “order” } @RequestMapping(value=”/order”, method = arrayOf(RequestMethod.POST)) fun […]

Kotlin数据类到JSON与spring/jackson

我试图公开一些数据类作为JSON对象,但有些不工作。 我有以下数据类: data class Link( @JsonProperty(“rel”) @JsonView(View.Bind::class) val rel: String, @JsonProperty(“method”) @JsonView(View.Bind::class) val method: HttpMethod, @JsonProperty(“href”) @JsonView(View.Bind::class) val href: String) data class MetaData(val status: HttpStatus) { @JsonView(View.Bind::class) @JsonProperty(“status_code”) fun getStatusCode(): Int { return status.value() } @JsonView(View.Bind::class) @JsonProperty(“status_desc”) fun getStatusDesc(): String { return status.name } } data class Payload( @JsonView(View.Bind::class) @JsonProperty(“payload”) val payload: Any, @JsonProperty(“_meta”) @JsonView(View.Bind::class) […]

逐渐填充InputStream并用SpringMVC返回

考虑我在数据库中有大量的csv字符串,我需要通过他的请求传递给客户端。 一个天真的方式来做到这一点: @RequestMapping(value = "/{user}/ejournal", method = RequestMethod.GET, produces = "text/csv") public ResponseEntity<ByteArrayResource> getEJournal(@PathVariable Long userId) { final byte[] documentBody = EJournal .findByUser(userId) .stream() .collect(Collectors.joining("\n")) .getBytes(); return new ResponseEntity( new ByteArrayResource(documentBody), HttpStatus.OK); } 从客户端直接(kotlin代码示例): val inputStream = eJournalService.getJournal(userId) inputStream.reader().forEachLine { line -> … } 但是我不得不把所有的数据加载到内存中,然后传递给流,显然效率不高。 所以我需要以某种方式缓冲它使用自定义阅读器分页读取数据和发送缓冲区到客户端。 我想实现我自己的InputStream但InputStream#read()返回int而不是String ,这有点复杂。 任何最佳实践来做到这一点? 我试图搜索,但只有例子传递一个图片使用已经在内存中的流,而不是缓冲分页后批次发送顺序数据库查询。 提前致谢。

Kotlin数据类到JSON与春天/杰克逊

我试图公开一些数据类作为JSON对象,但有些不工作。 我有以下数据类: data class Link( @JsonProperty("rel") @JsonView(View.Bind::class) val rel: String, @JsonProperty("method") @JsonView(View.Bind::class) val method: HttpMethod, @JsonProperty("href") @JsonView(View.Bind::class) val href: String) data class MetaData(val status: HttpStatus) { @JsonView(View.Bind::class) @JsonProperty("status_code") fun getStatusCode(): Int { return status.value() } @JsonView(View.Bind::class) @JsonProperty("status_desc") fun getStatusDesc(): String { return status.name } } data class Payload( @JsonView(View.Bind::class) @JsonProperty("payload") val payload: Any, @JsonProperty("_meta") @JsonView(View.Bind::class) […]

覆盖一些控制器的@JsonIgnore注释。 杰克逊。 弹簧

我只需要将照片字段仅用于直接请求到实体。 但是,当用户向所有MediaHolders对象发出请求时,仅使用photoSize序列化短信息。 我正在使用Kotlin,Jackson,Spring和Hibernate。 我试过了: 在控制器上的@JsonView。 不起作用。 JsonView不覆盖@JsonIgnore的行为。 或者我需要注释基本PersonalDomainObject类和所有控制器中的所有字段,这是不适合我。 自定义JsonFilter,但相同,它不会看到忽略字段。 而且我不能在不同的控制器上添加不同的过滤器。 也许我错过了一些东西,但我认为这是优化中的常见任务。 非常感谢您的任何建议! @JsonIgnoreProperties("createdAt", "updatedAt", "owner", "hibernateLazyInitializer", "handler") open class MediaHolder : PersonalDomainObject() { @OneToMany(cascade = arrayOf(CascadeType.PERSIST, CascadeType.REMOVE), mappedBy = "mediaHolder") @LazyCollection(LazyCollectionOption.EXTRA) var photos:MutableList<Photo> = mutableListOf() val photosSize: Int get() = photos.size }

文件上传功能Webflux

我使用“Router Kotlin DSL”将原型迁移到Spring WebFlux(和Spring Boot)的功能变体。 现在我需要一些MIME类型multipart/form-data文件上传提示,而不是像application/json使用一些数据。 当我定义一个简单的路由器函数和一个像这样的处理程序时,我得到如下的堆栈跟踪: fun routes(handler: MultimediaHandler) = router { (accept(MediaType.MULTIPART_FORM_DATA) and "/multimedia").nest { PUT("/{id}", handler::upload) } } class MultimediaHandler { fun upload(request: ServerRequest): Mono<ServerResponse> { val id = request.pathVariable("id") return noContent().build() } } 堆栈跟踪: 13:43:29.713 ERROR [iurequest] [XNIO-1 I/O-3] UT005071: Undertow request failed HttpServerExchange{ PUT /multimedia/00000000-0000-0000-0000-000000000 001 request {Connection=[Keep-Alive], Accept-Encoding=[gzip,deflate], Content-Length=[1672], […]

集成HTML和CSS与Kotlin和Spring MVC

我想为我的spring项目添加一些html和css。 我正在运行gradle,并使用Kotlin。 我目前的树目录是这样的: 链接 (我没有包括gradle构建文件)。 我只是试图打印“Hello $ name $”给一些输入的网址。 这工作。 这里是GreetingController.kt: @RestController class GreetingController { @RequestMapping("/greeting") fun greeting(@RequestParam(value = "name", defaultValue = "World") name: String, model: Model) { model.addAttribute("name", name); return "greeting"; } } 我的gradle.build文件: buildscript { ext.kotlin_version = '1.1.2' // Required for Kotlin integration ext.spring_boot_version = '1.5.3.RELEASE' repositories { jcenter() mavenCentral() } dependencies { […]