Questions tagged «java»

Java是一种流行的高级编程语言。如果您在使用或理解语言本身时遇到问题,请使用此标签。这个标签很少单独使用,最常与[spring],[spring-boot],[jakarta-ee],[android],[javafx],[gradle]和[maven]结合使用。



9
如何将标头添加到OkHttp请求拦截器?
我将这个拦截器添加到我的OkHttp客户端中: public class RequestTokenInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); // Here where we'll try to refresh token. // with an retrofit call // After we succeed we'll proceed our request Response response = chain.proceed(request); return response; } } 如何在拦截器中添加标题以进行请求? 我尝试了这个,但是我犯错了,创建新请求时我丢失了请求: …

4
如何在Kotlin中创建匿名接口实例?
我有一个第三方Java库,该对象的接口如下: public interface Handler<C> { void call(C context) throws Exception; } 我如何像Java匿名类一样在Kotlin中简洁地实现它: Handler<MyContext> handler = new Handler<MyContext> { @Override public void call(MyContext context) throws Exception { System.out.println("Hello world"); } } handler.call(myContext) // Prints "Hello world"

9
如何在Spring MVC Controller获取呼叫中提取IP地址?
我正在从事Spring MVC控制器项目,在该项目中我从浏览器进行GET URL调用- 以下是我从浏览器发出GET调用的网址- http://127.0.0.1:8080/testweb/processing?workflow=test&conf=20140324&dc=all 下面是在浏览器中调用后调用的代码- @RequestMapping(value = "processing", method = RequestMethod.GET) public @ResponseBody ProcessResponse processData(@RequestParam("workflow") final String workflow, @RequestParam("conf") final String value, @RequestParam("dc") final String dc) { System.out.println(workflow); System.out.println(value); System.out.println(dc); // some other code } 问题陈述:- 现在有什么方法可以从某些标头中提取IP地址吗?这意味着我想知道从哪个IP地址发出呼叫,这意味着无论谁在URL上方进行呼叫,我都需要知道其IP地址。这可能吗?

17
如何在我的application.properties文件的Spring Boot应用程序中配置HikariCP?
我试图在我的Spring Boot(1.2.0.M1)应用程序中设置HikariCP,以便可以测试使用它代替Tomcat DBCP。我想像在使用Tomcat一样在我的application.properties文件中配置连接池,但是我不知道该怎么做。我发现的所有示例都显示JavaConfig样式,或使用单独的HikariCP属性文件。有人可以帮我找出属性名称以在application.properties中进行配置吗?我也想从使用driverClassName方法切换到DataSourceClassName方法,因为它看起来更干净,建议使用。我的application.properties文件中也可能吗? 这是我对Tomcat DBCP的使用(只是一些基本配置,尚未完全清除) spring.datasource.validation-query=SELECT 1 spring.datasource.max-active=10 spring.datasource.max-idle=8 spring.datasource.min-idle=8 spring.datasource.initial-size=5 spring.datasource.test-on-borrow=true spring.datasource.test-on-return=true 而且我目前正在使用driverClassName和jdbc url来建立连接: spring.datasource.url=jdbc:mysql://localhost:3306/myDb spring.datasource.driverClassName=com.mysql.jdbc.Driver

10
在Java中的方法调用接受的实践中传递“ this”
在方法调用中传递当前对象是好/坏/可接受的做法。如: public class Bar{ public Bar(){} public void foo(Baz baz){ // modify some values of baz } } public class Baz{ //constructor omitted public void method(){ Bar bar = new Bar(); bar.foo(this); } } 具体来说,该行bar.foo(this)可接受吗?
93 java 

5
Gradle-获取依赖项的最新发行版
告诉Gradle以下内容的最简单方法是: 检索“ junit”依赖项并获取其最新的“发行版”版本。 管理Maven和Ivy存储库对我来说是新的。我尝试了以下步骤,但它们导致Could not resolve dependency ...错误: compile "junit:junit:latest.release"使用仅设置为(但是,如果我说“ junit:junit:4.10”)的存储库写mavenCentral() 。 compile "junit:junit:latest.release"使用存储库设置以下列方式写入: ivy { // I also tried 'http://maven.org' and other possible variants. url "http://repo1.maven.org" layout "maven" } 尝试使用Spring Source Ivy存储库: ivy { artifactPattern "http://repository.springsource.com/ivy/libraries/release/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]" ivyPattern "http://repository.springsource.com/ivy/libraries/release/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]" } 也许我误会了一些东西。为什么要获得最新版本的依赖项是一项艰巨的任务?

6
Java中的大量数字
我将如何使用Java中的超大型数进行计算? 我已经尝试过了,long但是最大值达到了9223372036854754775807,当使用整数时,它不能保存足够的数字,因此对于我所需要的来说不够准确。 有没有办法解决?

4
CacheProvider的异常NoClassDefFoundError
我在Spring和Hibernate中是新手,因此我尝试基于Spring 3 + Hibernate 4来实现一些简单的Web应用程序,而当我启动tomcat时,我有以下异常: java.lang.NoClassDefFoundError: org/hibernate/cache/CacheProvider at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:2427) at java.lang.Class.getDeclaredMethods(Class.java:1791) ... Caused by: java.lang.ClassNotFoundException: org.hibernate.cache.CacheProvider at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523) 我发现该类在hibernate 3的hibernate-core中,但是在hibernate 4中却找不到它。 我的context.xml中用于持久性的部分: <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="org.hsqldb.jdbcDriver"/> <property name="url" value="jdbc:oracle:thin:@IP_Address:SID"/> <property name="username" value="xxx"/> <property name="password" value="xxx"/> <property name="initialSize" value="5"/> <property name="maxActive" value="20"/> </bean> <bean …


6
在Eclipse上进行测试时如何传递-D系统属性?
我在Windows上的Eclipse上进行开发,并且代码在Unix上进行了部署。我正在使用System.getProperty(“ key”)获取系统属性值...如何在Eclipse中传递它,这样我就不必修改代码,并且可以在Eclipse上进行调试? 有什么建议?
93 java  eclipse 

3
FixedThreadPool与CachedThreadPool:两种弊端中的较小者
我有一个程序产生线程(〜5-150)来执行一堆任务。最初,我使用a,FixedThreadPool因为这个类似的问题表明它们更适合寿命更长的任务,并且由于我对多线程的了解非常有限,我认为线程的平均寿命(几分钟)是“ 寿命长 ”的。 但是,我最近添加了产生更多线程的功能,并且这样做使我超出了我设置的线程限制。在这种情况下,最好猜测并增加我可以允许的线程数,或者切换到a CachedThreadPool以便没有浪费的线程? 初步尝试将它们都尝试一下,似乎没有什么区别,所以我倾向于选择CachedThreadPool正义以避免浪费。但是,线程的寿命是否意味着我应该改而选择a FixedThreadPool并只处理未使用的线程?这个问题使得它看起来像那些额外的线程不被浪费,但我希望澄清。


15
Android:Dex无法解析版本52字节代码
我刚切换到Android Studio 2.1,并且在尝试编译以前可以正常使用的应用程序时出现了此错误: Error:Error converting bytecode to dex: Cause: Dex cannot parse version 52 byte code. This is caused by library dependencies that have been compiled using Java 8 or above. If you are using the 'java' gradle plugin in a library submodule add targetCompatibility = '1.7' sourceCompatibility = '1.7' …

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.