我必须用 StAX 解析 XML 文件。
我发现了一堆异常:
javax.xml.stream.XMLStreamException: java.net.MalformedURLException
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.setInputSource(XMLStreamReaderImpl.java:217)
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.<init>(XMLStreamReaderImpl.java:189)
at com.sun.xml.internal.stream.XMLInputFactoryImpl.getXMLStreamReaderImpl(XMLInputFactoryImpl.java:262)
at com.sun.xml.internal.stream.XMLInputFactoryImpl.createXMLStreamReader(XMLInputFactoryImpl.java:129)
at com.epam.lab.StaxXmlParser.<init>(StAXParserDemo.java:46)
at com.epam.lab.StAXParserDemo.main(StAXParserDemo.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.net.MalformedURLException
at java.net.URL.<init>(URL.java:619)
at java.net.URL.<init>(URL.java:482)
at java.net.URL.<init>(URL.java:431)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:610)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startEntity(XMLEntityManager.java:1290)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startDocumentEntity(XMLEntityManager.java:1242)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.setInputSource(XMLDocumentScannerImpl.java:257)
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.setInputSource(XMLStreamReaderImpl.java:204)
Run Code Online (Sandbox Code Playgroud)
这是 xml 文件的外观:
<?xml version="1.0" encoding="UTF-8"?>
<staff xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="newEmployee.xsd">
<employee>
<name>Carl Cracker</name>
<salary>75000</salary>
<hiredate year="1987" month="12" day="15" />
</employee>
<employee>
<name>Harry Hacker</name>
<salary>50000</salary>
<hiredate year="1989" month="10" day="1" />
</employee>
<employee> …Run Code Online (Sandbox Code Playgroud) 我正在工作或了解如何创建一个简单的java 2d迷宫,它应该如下所示:
int [][] maze =
{ {1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,1,0,1,0,1,0,0,0,0,0,1},
{1,0,1,0,0,0,1,0,1,1,1,0,1},
{1,0,0,0,1,1,1,0,0,0,0,0,1},
{1,0,1,0,0,0,0,0,1,1,1,0,1},
{1,0,1,0,1,1,1,0,1,0,0,0,1},
{1,0,1,0,1,0,0,0,1,1,1,0,1},
{1,0,1,0,1,1,1,0,1,0,1,0,1},
{1,0,0,0,0,0,0,0,0,0,1,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1}
};
Run Code Online (Sandbox Code Playgroud)
这个已被创建的想法是设置一个起点和目标点,并通过使用递归深度首先找到路径.但必须说我有困难创造迷宫.
你有什么建议怎么做吗?
或者也许是教程的链接?
我现在主要关注的是创造迷宫.
我想为控制器编写测试。这是测试代码段:
@RunWith(SpringRunner.class)
@WebMvcTest(WeatherStationController.class)
@ContextConfiguration(classes = MockConfig.class)
public class WeatherStationControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private IStationRepository stationRepository;
@Test
public void shouldReturnCorrectStation() throws Exception {
mockMvc.perform(get("/stations")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
}
Run Code Online (Sandbox Code Playgroud)
控制器代码段:
@RestController
@RequestMapping(value = "stations")
public class WeatherStationController {
@Autowired
private WeatherStationService weatherService;
@RequestMapping(method = RequestMethod.GET)
public List<WeatherStation> getAllWeatherStations() {
return weatherService.getAllStations();
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public WeatherStation getWeatherStation(@PathVariable String id) {
return weatherService.getStation(id);
}
Run Code Online (Sandbox Code Playgroud)
MockConfig类:
@Configuration
@ComponentScan(basePackages = "edu.lelyak.repository")
public class MockConfig {
//**************************** MOCK BEANS …Run Code Online (Sandbox Code Playgroud) 在大多数编程语言中,开发人员有责任编写不可变的单例类等。
但是,我觉得这是重复的样板代码。更新的编程语言为实现此目的提供了更好的支持。
Java 9是否引入了任何注释或类似构造来将类标记为不可变或单例?
只需在本地机器(Linux Mint 20.2)上研究K8S的核心即可。
使用以下命令在本地创建一个节点集群:
k3d集群创建mycluster
现在我想在容器中运行 spring boot 应用程序。
我构建本地图像:
库:0.1.0
这是摘自Deployment.yml:
spec:
terminationGracePeriodSeconds: 40
containers:
- name: 'library'
image: library:0.1.0
imagePullPolicy: IfNotPresent
Run Code Online (Sandbox Code Playgroud)
尽管图像已经构建:
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
library 0.1.0 254c13416f46 About an hour ago 462MB
Run Code Online (Sandbox Code Playgroud)
启动容器失败:
pod/library-867dfb64db-vndtj Pulling image "library:0.1.0"
pod/library-867dfb64db-vndtj Failed to pull image "library:0.1.0": rpc error: code = Unknown desc = failed to pull and unpack image "library:0.1.0": failed to resolve reference "library:0.1.0": failed to do request: Head "https://...com/v2/library/manifests/0.1.0": x509: certificate …Run Code Online (Sandbox Code Playgroud) 我发现了一种在 Java 中使用switch语句的有趣方法,但我无法捕获所有逻辑。
有人可以帮助深入了解所有细节吗?
这是代码:
private static int counter = 0;
public static Shape randomFactory() {
int xVal = rand.nextInt(100);
int yVal = rand.nextInt(100);
int dim = rand.nextInt(100);
switch (counter++ % 3) {
default:
case 0:
return new Circle(xVal, yVal, dim);
case 1:
return new Square(xVal, yVal, dim);
case 2:
return new Line(xVal, yVal, dim);
}
}
Run Code Online (Sandbox Code Playgroud)
一般来说我理解这个逻辑,
这里的确切含义是default:
switch (counter++ % 3) {
default:
Run Code Online (Sandbox Code Playgroud)
以及如何switch (counter++ % 3)查找等于大小写?这里没有任何brake介绍。
有什么建议么?
我必须用 stax 解析 xml 文件,除了一点之外,我基本上做了我需要的事情。
我的代码逻辑没有提取正确的属性。我的输出如下:
Employee { name=Carl Cracker, salary=75000.0, hireDay=null }
Employee { name=Harry Hacker, salary=50000.0, hireDay=null }
Employee { name=Tony Tester, salary=40000.0, hireDay=null }
Run Code Online (Sandbox Code Playgroud)
这是xml文件的内容:
<?xml version="1.0" encoding="UTF-8"?>
<staff xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="newEmployee.xsd">
<employee>
<name>Carl Cracker</name>
<salary>75000</salary>
<hiredate year="1987" month="12" day="15" />
</employee>
<employee>
<name>Harry Hacker</name>
<salary>50000</salary>
<hiredate year="1989" month="10" day="1" />
</employee>
<employee>
<name>Tony Tester</name>
<salary>40000</salary>
<hiredate year="1990" month="3" day="15" />
</employee>
</staff>
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
class StaxXmlParser {
private List<Employee> employeeList;
private Employee currentEmployee;
private String tagContent;
private XMLStreamReader …Run Code Online (Sandbox Code Playgroud) 我不太明白我可以在哪里抛出这个异常.
例如,我正在实现Future<T>接口,并且不希望任何人调用该方法:
那么,我可以扔掉UnsupportedOperationException吗?
public T get(long timeout, TimeUnit unit){
throw new UnsupportedOperationException();
}
Run Code Online (Sandbox Code Playgroud)
事情是该方法的规范没有说什么关于抛出异常.反过来,例外
抛出以指示不支持所请求的操作.
Class UnsupportedOperationException
我的意思是,如果您不希望它被调用或者它可能被认为是不正确的,因为并非所有方法都已实现,它是否常见?在我的具体情况下,我认为调用该方法没有意义......
我不知道如何在 React-Native 中使用布尔值。
我试图在 google 上找到解决方案,但显然我的问题太简单了,没有人问过它。
我的代码必须在哪里声明布尔值?
如何更改值?
比较它的正确方法是什么?(我认为 if(myBoolean ===true) 但我不完全确定。)
我有Spring Boot项目,EnvironmentPostProcessor实现简单:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
public class DevProfilerResolverEnvironmentPostProcessor implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment, SpringApplication springApplication) {
if (configurableEnvironment.getActiveProfiles().length == 0) {
if (System.getenv().get("OS").contains("Windows")) {
configurableEnvironment.addActiveProfile("DEV");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
另外,我将这个课程注册到sprig.factories:
org.springframework.boot.env.EnvironmentPostProcessor = com.example.demo.DevProfilerResolverEnvironmentPostProcessor
Run Code Online (Sandbox Code Playgroud)
现在结构看起来像:
来自pom文件的片段:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
Run Code Online (Sandbox Code Playgroud)
我和Maven一起执行:
mvn安装 …
java ×7
spring-boot ×3
stax ×2
xml ×2
xml-parsing ×2
boolean ×1
controller ×1
docker ×1
exception ×1
immutability ×1
interface ×1
java-9 ×1
javascript ×1
k3d ×1
kubernetes ×1
linux ×1
maven ×1
maze ×1
react-native ×1
singleton ×1
spring ×1
unit-testing ×1