在Tomcat和Glassfish上运行时,我遇到运行相同代码的返回列表的json结构的差异.
@XmlRootElement
public Class Person {
private int personId;
private String name;
}
@GET
@Path("/persons")
public Response getPersons()
{
List<Person> persons = new ArrayList<Person>();
persons.add(new Person(1, "John Smith"));
persons.add(new Person(2, "Jane Smith"));
GenericEntity<List<Person>> entity = new GenericEntity<List<Person>>(Lists.newArrayList<persons)) {};
Return Response.ok(entity).build();
}
Run Code Online (Sandbox Code Playgroud)
如果在tomcat上以json格式返回(我在本地机器上运行Tomcat),结果将是:
[
{
"personId" : "1",
"name" : "John Smith"
},
{
"personId" : "2",
"name" : "Jane Smith"
}
]
Run Code Online (Sandbox Code Playgroud)
如果在Glassfish上以json格式返回(在远程服务器上运行Glassfish),结果将是:
{
"person" :
[
{
"personId" : "1",
"name" : "John Smith"
},
{
"personId" : "2", …Run Code Online (Sandbox Code Playgroud) 我是 Java 8 和 Stream API 的新手。
如果我有一个 Employee 对象列表:
List<Employee> employees;
public class Employee {
private String name;
private List<Project> involvedInProjects;
}
public class Project {
private int projectId;
}
Run Code Online (Sandbox Code Playgroud)
我想过滤参与某些项目的员工,我将如何使用 java 8 中的流 API 执行此操作?
如果我有一个 Map ,其中键是唯一的员工 ID 而不是 List 会更容易吗?
我需要能够在做一些工作时锁定 SQL Server 中的表。锁需要阻止其他会话读取该表。工作完成后,需要解锁桌子。
在 MySQL 中,我使用以下方法完成了此操作:
LOCK TABLES [table] WRITE
Run Code Online (Sandbox Code Playgroud)
此时,其他会话在尝试从表中读取数据时将被阻止,直到表被解锁。
UNLOCK TABLES
Run Code Online (Sandbox Code Playgroud)
现在封锁将停止。
这在 SQL Server 中可能吗?
在我的webapp中,我有3个线程,其中tomcat在重新加载时无法停止其中的2个.
严重:Web应用程序[/ myapp]似乎已经启动了一个名为[Thread-8]的线程,但未能阻止它.这很可能造成内存泄漏.mai 08,2013 11:22:40 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
这会导致每次重新加载时CPU使用率上升.
这是tomcat无法停止的一个线程:
我的ServletContextListener中实现的一些代码:
public void contextInitialized(ServletContextEvent event)
{
final UpdaterThread updaterThread = new UpdaterThread();
updaterThread.start();
event.getServletContext().setAttribute("updaterthread", updaterThread);
}
public void contextDestroyed(ServletContextEvent event)
{
UpdaterThread updaterThread = (UpdaterThread) event.getServletContext().getAttribute("updaterthread");
if (updaterThread != null)
{
updaterThread.stopUpdater();
updaterThread.interrupt();
updaterThread = null;
}
}
Run Code Online (Sandbox Code Playgroud)
以及UpdaterThread的重要部分:
public class UpdaterThread extends Thread implements Runnable
{
private boolean alive = true;
@Override
public void run()
{
while(true)
{
try
{
while (alive)
{
doUpdate();
sleep(60*1000);
}
}
catch (InterruptedException …Run Code Online (Sandbox Code Playgroud) 我是Slick的新手,很难将java.sql.date/time/timestamp的映射映射到jodatime.
trait ColumnTypeMappings {
val profile: JdbcProfile
import profile.api._
val localTimeFormatter = DateTimeFormat.forPattern("HH:mm:ss")
val javaTimeFormatter = new SimpleDateFormat("HH:mm:ss")
implicit val myDateColumnType = MappedColumnType.base[LocalDate, Date](
ld => new java.sql.Date(ld.toDateTimeAtStartOfDay(DateTimeZone.UTC).getMillis),
d => new LocalDateTime(d.getTime).toLocalDate
)
implicit val myTimeColumnType = MappedColumnType.base[LocalTime, Time](
lt => new java.sql.Time(javaTimeFormatter.parse(lt.toString(localTimeFormatter)).getTime),
t => new LocalTime(t.getTime)
)
implicit val myTimestampColumnType = MappedColumnType.base[DateTime, Timestamp](
dt => new java.sql.Timestamp(dt.getMillis),
ts => new DateTime(ts.getTime, DateTimeZone.UTC)
)
}
Run Code Online (Sandbox Code Playgroud)
在自动生成的Tables.scala中我包含这样的映射:
trait Tables extends ColumnTypeMappings {
val profile: slick.driver.JdbcDriver
import profile.api._
import scala.language.implicitConversions
// + …Run Code Online (Sandbox Code Playgroud) 我有一个Service类和一个主Acitivity类,应该使用sendBroadcast方法从Service类接收广播。
sendBroadcast从我的Service类中运行方法时崩溃。
这是我的Service类(EDITED)的一部分:
public static final int STATE_CONNECTING = 1;
public static final String BT_CONNECTING = "com.android.mypackage.action.BTService.BT_CONNECTING";
private final IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder() {
BTService getService() {
return BTService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public synchronized void setState(int state) {
mState = state;
if ( state == STATE_CONNECTING ) {
Intent myIntent = new Intent(BT_CONNECTING);
try {
sendBroadcast(myIntent);
} …Run Code Online (Sandbox Code Playgroud)