Questions tagged «java»

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


9
什么是持久性上下文?
我是Java世界和JPA的新手。我在学习JPA时遇到了许多新术语,例如Entity,persistence。在阅读时,我无法理解Persistence Context的确切定义。 谁能用简单的外行术语解释它?与中使用的数据有@Entity什么关系? 例如,我发现此定义太复杂而难以理解: 持久性上下文是一组实体,因此对于任何持久性身份,都有一个唯一的实体实例。
109 java  jpa  orm  persistence 


6
如何在Java中修改JsonNode?
我需要在Java中更改JSON属性的值,我可以正确获取该值,但无法修改JSON。 这是下面的代码 JsonNode blablas = mapper.readTree(parser).get("blablas"); for (JsonNode jsonNode : blablas) { String elementId = jsonNode.get("element").asText(); String value = jsonNode.get("value").asText(); if (StringUtils.equalsIgnoreCase(elementId, "blabla")) { if(value != null && value.equals("YES")){ // I need to change the node to NO then save it into the JSON } } } 做这个的最好方式是什么?
109 java  json  jackson 

9
什么是JAXB,为什么要使用它?[关闭]
已关闭。这个问题是基于观点的。它当前不接受答案。 想改善这个问题吗?更新问题,以便通过编辑此帖子以事实和引用的形式回答。 3年前关闭。 改善这个问题 这里有人发誓说JAXB是切面包以来最伟大的事情。我很好奇看到Stack Overflow用户认为用例适用于JAXB,以及使该用例成为好方案还是不好的解决方案。
109 java  jaxb 

8
为什么jUnit的fixtureSetup必须是静态的?
我用jUnit的@BeforeClass批注标记了一个方法,并得到了这个异常,说它必须是静态的。有什么根据?据我所知,这没有任何充分理由就迫使我的所有init都处于静态字段上。 在.Net(NUnit)中,情况并非如此。 编辑 -使用@BeforeClass注释的方法只能运行一次的事实与它作为静态方法无关-可以使非静态方法仅运行一次(与NUnit一样)。
109 java  junit 

7
从HttpURLConnection获取InputStream对象时发生FileNotFoundException
我正在尝试使用HttpURLConnection(在Java中使用cUrl)将发布请求发送到url。请求的内容为xml,最后,应用程序处理xml并将记录存储到数据库,然后以xml字符串的形式发送回响应。该应用程序本地托管在apache-tomcat上。 当我从终端执行此代码时,将按预期将一行添加到数据库。但是,从连接获取InputStream时会引发如下异常 java.io.FileNotFoundException: http://localhost:8080/myapp/service/generate at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1401) at org.kodeplay.helloworld.HttpCurl.main(HttpCurl.java:30) 这是代码 public class HttpCurl { public static void main(String [] args) { HttpURLConnection con; try { con = (HttpURLConnection) new URL("http://localhost:8080/myapp/service/generate").openConnection(); con.setRequestMethod("POST"); con.setDoOutput(true); con.setDoInput(true); File xmlFile = new File("test.xml"); String xml = ReadWriteTextFile.getContents(xmlFile); con.getOutputStream().write(xml.getBytes("UTF-8")); InputStream response = con.getInputStream(); BufferedReader reader = new …

3
如何在Android应用程序中激活“共享”按钮?
我想向我的Android应用程序添加“共享”按钮。 像那样 我添加了“共享”按钮,但该按钮未激活。我单击,但什么都没有发生。 我在MainActivity.java中的代码: private ShareActionProvider mShareActionProvider; @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.share_menu, menu); getMenuInflater().inflate(R.menu.main, menu); MenuItem item = menu.findItem(R.id.share_menu); mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share_menu).getActionProvider(); mShareActionProvider.setShareIntent(getDefaultShareIntent()); return true; } { Intent sharingIntent = new Intent(Intent.ACTION_SEND); sharingIntent.setType("text/plain"); sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Text"); sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject"); startActivity(Intent.createChooser(sharingIntent, "Share using")); } 我想在第一个选项卡(first_tab.xml)或第二个选项卡(second_tab.xml)中共享文本。 标签(xml)中的代码(如果需要): <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" …
109 java  android  xml  button  share 

7
以编程方式更改Log4j2中的日志级别
我对以编程方式更改Log4j2中的日志级别感兴趣。我尝试查看他们的配置文档,但似乎没有任何内容。我也尝试在包中查找:org.apache.logging.log4j.core.config,但其中的任何内容看起来都没有帮助。
109 java  log4j2 


13
在GAE上解析完全有效的XML时,“序言中不允许内容”
在过去的48个小时里,我一直对这个绝对令人毛骨悚然的bug感到震惊,所以我想我最终会把毛巾扔掉,尝试在这里问一下,然后再将笔记本电脑扔出窗户。 我正在尝试从对AWS SimpleDB的调用中解析响应XML。响应恢复正常。例如,它可能看起来像: <?xml version="1.0" encoding="utf-8"?> <ListDomainsResponse xmlns="http://sdb.amazonaws.com/doc/2009-04-15/"> <ListDomainsResult> <DomainName>Audio</DomainName> <DomainName>Course</DomainName> <DomainName>DocumentContents</DomainName> <DomainName>LectureSet</DomainName> <DomainName>MetaData</DomainName> <DomainName>Professors</DomainName> <DomainName>Tag</DomainName> </ListDomainsResult> <ResponseMetadata> <RequestId>42330b4a-e134-6aec-e62a-5869ac2b4575</RequestId> <BoxUsage>0.0000071759</BoxUsage> </ResponseMetadata> </ListDomainsResponse> 我将此XML传递给解析器 XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(response.getContent()); 并打电话eventReader.nextEvent();多次以获得我想要的数据。 这是奇怪的部分-它在本地服务器中很好用。我解析了响应,每个人都很高兴。问题是,当我将代码部署到Google App Engine时,传出请求仍然有效,并且响应XML对我来说似乎100%相同且正确,但是响应无法解析,但出现以下异常: com.amazonaws.http.HttpClient handleResponse: Unable to unmarshall response (ParseError at [row,col]:[1,1] Message: Content is not allowed in prolog.): <?xml version="1.0" encoding="utf-8"?> …


21
将秒值转换为小时分钟秒?
我一直在尝试将秒值(在BigDecimal变量中)转换为editText之类的字符串,例如“ 1小时22分33秒”或类似的内容。 我已经试过了: String sequenceCaptureTime = ""; BigDecimal roundThreeCalc = new BigDecimal("0"); BigDecimal hours = new BigDecimal("0"); BigDecimal myremainder = new BigDecimal("0"); BigDecimal minutes = new BigDecimal("0"); BigDecimal seconds = new BigDecimal("0"); BigDecimal var3600 = new BigDecimal("3600"); BigDecimal var60 = new BigDecimal("60"); (我有一个roundThreeCalc,它是以秒为单位的值,所以我尝试在这里进行转换。) hours = (roundThreeCalc.divide(var3600)); myremainder = (roundThreeCalc.remainder(var3600)); minutes …



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.