我正在使用Spring Boot构建一个应用程序.这个应用程序是分布式的,这意味着我有多个API相互调用.
我的一个底层服务与数据库交互并使用请求的数据进行响应.如果对未存在的ID发出请求,我会回复404 HttpStatus:
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
Run Code Online (Sandbox Code Playgroud)
(与某些操作中的400错误相同,或者删除条目时为204).
问题是我有一些其他的Spring Boot应用程序调用这些API,org.springframework.web.client.HttpClientErrorException: 404 Not Found在他们请求时抛出一个Exception,在这个例子中,是一个不存在的条目.但404状态代码是有意的,不应该返回此异常(导致我的Hystrix断路器调用其回退功能).
我怎么解决这个问题?
在我的代码中实现对服务的调用: ResponseEntity<Object> data = restTemplate.getForEntity(url, Object.class);
我的RestTemplate设置如下:
private RestTemplate restTemplate = new RestTemplate();
Run Code Online (Sandbox Code Playgroud) 我正在尝试为这个Java SpringBoot类编写一个测试:
具体来说,我试图"模拟"这个方法调用:
URI uri = util.getServiceUrl("product");
Run Code Online (Sandbox Code Playgroud)
我想我应该"模仿" ServiceUtils对象才能做到这一点.我使用@Mock和@InjectMocks注释尝试了这个:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ProductCompositeServiceApplication.class)
public class ProductCompositeIntegrationTest {
@InjectMocks
@Autowired
private ProductCompositeIntegration productIntegration;
@Autowired
private RestTemplate restTemplate;
@Mock
private ServiceUtils util;
private MockRestServiceServer mockServer;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mockServer = MockRestServiceServer.createServer(restTemplate);
}
@Test
public void myTest() {
Mockito.when(util.getServiceUrl("product")).thenReturn(URI.create("http://localhost:8080/test"));
ResponseEntity<Iterable<Product>> products = productIntegration.getAllProducts();
}
}
Run Code Online (Sandbox Code Playgroud)
但是这样它仍然会调用原始ServiceUtils对象,而不是"模拟"对象.也试过没有@Autowired注释ProductCompositeIntegration,但这导致了一个NullPointerException.
我究竟做错了什么?
我的主要课程如下:
@SpringBootApplication
@EnableCircuitBreaker
@EnableDiscoveryClient
public class ProductCompositeServiceApplication {
public …Run Code Online (Sandbox Code Playgroud) 我正在使用casablanca C++ Rest库来发出HTTP请求.
问题是这给了一个utility :: string_t字符串作为输出,我无法找到任何方法将其转换为经典的std :: string.有任何想法吗?
client.request(methods::GET).then([](http_response response)
{
if(response.status_code() == status_codes::OK)
{
string_t s = response.extract_string().get();
}
});
Run Code Online (Sandbox Code Playgroud) 我正在尝试用maven构建我的java hibernate项目.但是当我尝试这样做时,看起来似乎没有可用的依赖?
我现在在我的项目中有这个pom.xml:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.viralpatel.hibernate</groupId>
<artifactId>HibernateHelloWorldXML</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>HibernateHelloWorldXML</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>ejb3-persistence</artifactId>
<version>1.0.1.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.3.1.GA</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.10</version>
</dependency>
</dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)
当我尝试使用mvn构建时,我得到此错误:
[WARNING] An error occurred during dependency resolution.
Failed to retrieve javax.transaction:jta-1.0.1B
Caused by: Failure to find javax.transaction:jta:jar:1.0.1B in http://repo.maven.apache.org/maven2 was cached in the local repository, resolution will
not be reattempted until the update interval of central has elapsed or updates are forced
Try downloading the file …Run Code Online (Sandbox Code Playgroud) 我在我的班级'Bomb'的Java中有一个ArrayList.
这个类有一个方法'isExploded',如果炸弹爆炸,这个方法将返回true,否则返回false.
现在我正在尝试迭代这个arraylist,调用此方法isExploded并从列表中删除该元素,如果它返回true.
我知道如何迭代:
for (Iterator i = bombGrid.listIterator(); i.hasNext();) {
if () {
i.remove();
}
Run Code Online (Sandbox Code Playgroud)
但我不知道如何通过迭代器访问Bomb类本身的方法isExploded.有谁知道答案吗?
此致
卢克索
我正在尝试用Java(学校项目)制作游戏,我有以下设置:
一个主要的类,用JFrame扩展,一个'Game'类,用JPanel扩展.
现在从这个主类中,我调用了一个类'Player'和一个类'Map'.类'Map'存在两个子类'Blocks'和'Bombs'.
但我想知道..我如何让所有这些类的绘制方法绘制到相同的JPanel(类Game)?
我给每个类的方法'public void paint(Graphics g)'并做了绘画..但是当我运行程序时,只有"Game"类的绘画出现,而不是来自子类的绘画.
我该如何实现?
例如,我将代码缩减为:
主要课程:
BomberGame game = new BomberGame();
add(game);
setSize(400, 400);
setTitle("Bomber");
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.show();
}
public static void main(String[] args) {
BomberB1 main = new BomberB1();
}
}
Run Code Online (Sandbox Code Playgroud)
游戏类:
package bomberb1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class BomberGame extends JPanel {
public BomberGame() {;
BomberMap map = new …Run Code Online (Sandbox Code Playgroud) java ×5
spring-boot ×2
arraylist ×1
c++ ×1
casablanca ×1
eclipse ×1
graphics ×1
hibernate ×1
iterator ×1
maven ×1
mockito ×1
rest ×1
spring ×1
spring-mvc ×1
string ×1
swing ×1
unit-testing ×1