尝试制作Play Framework(REST)并ng-bootstrap - Typeahead一起工作.但我面临从json响应中提取数据的问题.例如我写"test"(在数据库中搜索name),服务器返回json数组(一切都正确):
[{
"id": 1,
"name": "test",
"annotation": "test annotation",
"kiMin": 1,
"kiMax": 2,
"cosFiMin": 3,
"cosFiMax": 4
}, {
"id": 4,
"name": "test2",
"annotation": "test annotation",
"kiMin": 1,
"kiMax": 2,
"cosFiMin": 3,
"cosFiMax": 4
}]
Run Code Online (Sandbox Code Playgroud)
但视图看起来像这样:
这是我的代码:
http.service.ts
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Equipment } from './equipment';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class HttpService {
private Url = …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用注释DataSource从Tomcat 8.5.56注入@Resource,但是当我运行代码时,我得到 NullPointerException。
注:获得DataSource与InitialContext仍正常工作。
中的资源定义context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/postgres" auth="Container"
type="javax.sql.DataSource" driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://localhost:5432/contact_directory"
username="username" password="password" maxTotal="20" maxIdle="10"
maxWaitMillis="-1"/>
</Context>
Run Code Online (Sandbox Code Playgroud)
用于测试的 servlet:
@WebServlet(name = "TestController", urlPatterns = "/test")
public class TestController extends HttpServlet {
//this also doesn't work
//@Resource(lookup="java:/comp/env/jdbc/postgres")
@Resource(name="jdbc/postgres")
private DataSource dataSource;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String query = "SELECT ... FROM ..."; //here goes the query text …Run Code Online (Sandbox Code Playgroud) 在切换到不同的 git 分支后,我在构建使用 Lombok 的项目时遇到了问题。我通常会收到这两种类型的多个异常:
@Setter(onMethod_ = @Autowired)
public class ClassA{
private ClassC c;
}
Run Code Online (Sandbox Code Playgroud)
我得到
java: cannot find symbol
symbol: method onMethod_()
location: @interface lombok.Setter
Run Code Online (Sandbox Code Playgroud)
@Builder
public class ClassB{
}
Run Code Online (Sandbox Code Playgroud)
我得到
java: cannot find symbol
symbol: class ClassBBuilder
location: class com.example.application.ClassB
Run Code Online (Sandbox Code Playgroud)
在像这样的方法中
private ClassB.ClassBBuilder getBuilder(Object input) {
//builder init
}
Run Code Online (Sandbox Code Playgroud)
StackOverflowError。运行gradle:clean->后问题已解决gradle:build。但是在交换分支后又出现了。更多信息:我正在使用Intellij Idea 2020.3.3 Ultimate Edition并选中复选框"Enable Annotation processing"。这是我的一些部分build.gradle:
import org.springframework.boot.gradle.plugin.SpringBootPlugin
plugins {
id 'net.ltgt.apt' version …Run Code Online (Sandbox Code Playgroud) 有没有办法像在 Java 中一样覆盖 Scala 3 枚举中的方法?
public enum Test {
ONE {
@Override
public int calc() {
return 1;
}
},
TWO {
@Override
public int calc() {
return 2;
}
};
public abstract int calc();
}
Run Code Online (Sandbox Code Playgroud)
我试过这样的事情,但没有结果。在文档中也没有找到关于枚举方法覆盖的任何内容。
enum Test {
def calc(): Int ={
0
}
case One
override def calc(): Int ={
1
}
case Two
override def calc(): Int ={
2
}
}
Run Code Online (Sandbox Code Playgroud)
也许有另一种方法可以实现类似的功能?
我怎样才能将一个json对象数组解析为scala List或Array?
现在我有一个解析单个对象的代码:
{"id":1,"name":"example1"}
Run Code Online (Sandbox Code Playgroud)
这是代码:
def exampleAction = Action.async(parse.json) { implicit request =>
for {
id <- (request.body \ "id").asOpt[Int]
name <- (request.body \ "name").asOpt[String]
} yield {
(exampleService.create(Example(id, name)) map { n => Created("Id of Object Added : " + n) }).recoverWith {
case e => Future {
InternalServerError("There was an error at the server")
}
}
}.getOrElse(Future { BadRequest("Wrong json format") })
}
Run Code Online (Sandbox Code Playgroud)
但是我应该如何更改它以解析像这样的json请求:
{[{"id":1,"name":"example1"},{"id":2,"name":"example2"}]}
Run Code Online (Sandbox Code Playgroud)
我想功能map应该在某个地方使用.
我正在尝试为我的一些enums使用default方法的接口添加默认方法。该方法应该检查是否enum在数组(varargs)中enum。
"Possible heap pollution from parameterized vararg type",但不是在 的情况下enum,因为它是final,对吗?"Unchecked cast: BaseEnum<E> to E"(并且"Suspicious call"警告没有演员)。在实现接口时传递正确的类型参数之前,它也是安全的。这是我的示例代码:public interface BaseEnum<E extends Enum<E>> {
@SuppressWarnings("unchecked")
default boolean in(E ... statuses){
return Arrays.asList(statuses)
.contains((E) this);
}
}
public enum Transport implements BaseEnum<Transport> {
CAR, BUS, PLANE
}
public enum Fruit implements BaseEnum<Fruit> {
APPLE, CHERRY, LEMON
}
Run Code Online (Sandbox Code Playgroud)
有了这个实现,一切看起来都很安全。但是我怎样才能防止这样的事情呢?(“防止”是指一些代码限制)
public enum Transport implements BaseEnum<Fruit> { …Run Code Online (Sandbox Code Playgroud) 在 Play 框架中,我将 Slick 与 MySQL 数据库一起使用,如何将查询结果(Future[Seq[SomeClass]])转换为 Json 以在 jQuery Autocomplete 中进一步使用。我可以序列化SomeClass,但是我应该在哪里使用.map(或其他东西)?
补充:
模型:
package models
import play.api.libs.json._
case class Equipment(id: Long, name: String,area: String,kiMin: Double,kiMax: Double,cosFiMin: Double,cosFiMax: Double){
implicit val equipmentWrites = new Writes[Equipment] {
def writes(equipment: Equipment) = Json.obj(
"id" -> equipment.id,
"name" -> equipment.name,
"area" -> equipment.area,
"kiMin" -> equipment.kiMin,
"kiMax" -> equipment.kiMax,
"cosFiMin" -> equipment.cosFiMin,
"cosFiMax" -> equipment.cosFiMax
)
//also tried this for Seq
/* def writes(equipment: Equipment): JsValue …Run Code Online (Sandbox Code Playgroud)