在主题中,我想将日期作为参数传递。我有这个依赖:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我的URL样子:
http://localhost:8080/userProducts/2?date=2019-3-29
Run Code Online (Sandbox Code Playgroud)
我的控制器类看起来像:
package trainingapp.userproduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import trainingapp.calculations.NutrientsCalculationFacade;
import trainingapp.historysystemofmeals.HistorySystemService;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
@RestController
public class UserProductController {
private final NutrientsCalculationFacade userProductCalculationFacade;
private final UserProductFindOperationService userProductFindOperationService;
private final HistorySystemService historySystemService;
@Autowired
public UserProductController(NutrientsCalculationFacade userProductCalculationFacade, UserProductFindOperationService userProductFindOperationService, HistorySystemService historySystemService) {
this.userProductCalculationFacade = userProductCalculationFacade;
this.userProductFindOperationService = userProductFindOperationService;
this.historySystemService = historySystemService;
}
//yyyy-MM-dd
@GetMapping("/userProducts/{userID}")
public String getAllEatenSummedNutrientsByGivenIDInParticularDay(@PathVariable int userID,
@RequestParam("date")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
return historySystemService.getAllEatenSummedNutrientsByGivenIDInParticularDay(userID, …Run Code Online (Sandbox Code Playgroud) 嘿,我遇到了与这里相同的问题:JSON Java 8 LocalDateTime format in Spring Boot我尝试了那里的解决方案,但它不起作用。有人可以告诉我我做错了什么吗?
\n\n我添加了
\n\nspring.jackson.serialization.write-dates-as-timestamps=false\nRun Code Online (Sandbox Code Playgroud)\n\n到 application.property\n我的模型类如下所示:
\n\npackage bookrental.model.book;\n\nimport bookrental.model.account.User;\nimport com.fasterxml.jackson.annotation.JsonFormat;\nimport com.fasterxml.jackson.databind.annotation.JsonDeserialize;\nimport com.fasterxml.jackson.databind.annotation.JsonSerialize;\nimport com.fasterxml.jackson.databind.util.ISO8601DateFormat;\nimport com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;\nimport com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;\nimport lombok.*;\n\nimport javax.persistence.*;\nimport java.time.LocalDateTime;\nimport java.util.Date;\n\n@Entity\n@Getter\n@Setter\n@EqualsAndHashCode\n@AllArgsConstructor\n@NoArgsConstructor\npublic class BookRentals {\n\n @Id\n @GeneratedValue(strategy = GenerationType.AUTO)\n private int id;\n @OneToOne\n private Book book;\n @OneToOne\n private User user;\n @JsonFormat(pattern = ("yyyy/MM/dd HH:mm:ss"))\n @JsonSerialize(using = LocalDateTimeSerializer.class)\n @JsonDeserialize(using = LocalDateTimeDeserializer.class)\n private LocalDateTime dateOfRental;\n\n public BookRentals(Book book, User user) {\n this.book = book;\n this.user = user;\n }\n\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我这样设定时间:
\n\nprivate BookRentals …Run Code Online (Sandbox Code Playgroud) 我试图测试负责从文件中检索数据的方法。我想测试是否正确抛出异常。
package contentfile;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class ContentFileRetrieverService implements ContentFileRetriever {
@Override
public String[] getContentFile(String pathName) {
Stream<String> contentFileStream;
try {
contentFileStream = Files.lines(Paths.get(pathName));
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
return contentFileStream.toArray(String[]::new);
}
}
Run Code Online (Sandbox Code Playgroud)
我的测试:
package contentfile;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.*;
class ContentFileRetrieverServiceTest {
private ContentFileRetrieverService contentFileRetrieverService = new ContentFileRetrieverService();
@Test
void getContentFile() {
String pathFile = "src\\test\\java\\resources\\TestText.txt";
String[] testedContent = contentFileRetrieverService.getContentFile(pathFile);
String[] expected = {"Line1 a", "Line2 b c", …Run Code Online (Sandbox Code Playgroud) 我是Spring Security的新人.如果我按下登录,则会发生以下网站:http:// localhost:8080/j_spring_security_check
HTTP Status 403 – Forbidden
Type Status Report
Message Forbidden
Description The server understood the request but refuses to authorize it.
Apache Tomcat/9.0.12
Run Code Online (Sandbox Code Playgroud)
这是web.xml
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/webcontext/security-context.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>DefaultServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/webcontext/DispatcherServlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DefaultServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
login.jsp
Run Code Online (Sandbox Code Playgroud)
在这里: <form action="<c:url value="/j_spring_security_check"></c:url>" method="post">- /j_spring_security_check标记在红色上并带有错误:Cannot resolve controller …
我尝试单元测试,如果方法负责返回给定产品的价格,如果我们通过坏抛出一个例外item_id- find_price_of_given_id。
测试:
import unittest
from Automat import Automat
from Bank import Bank
from Item import Item
from exceptions.NoItemException import NoItemException
class AutomatTest(unittest.TestCase):
def test_checkPriceOfGivenID(self):
bank = Bank()
automat = Automat(bank)
Cola = Item(2)
automat.add_object(Cola)
self.assertEqual(automat.find_price_of_given_id(30), 2)
def test_checkPriceOfGivenIDWithInvalidID(self):
bank = Bank()
automat = Automat(bank)
Cola = Item(2)
automat.add_object(Cola)
self.assertRaises(NoItemException, automat.find_price_of_given_id(31))
Run Code Online (Sandbox Code Playgroud)
自动垫类:
if __name__ == '__main__':
unittest.main()
from Item import Item
from exceptions.NoItemException import NoItemException
from exceptions.NoProperAmountException import NoProperAmountException
from Coin import Coin
from decimal import * …Run Code Online (Sandbox Code Playgroud) 我试图了解线程。我写了简单的程序。
public class Main {
static int counter = 0;
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
counter++;
}
});
Thread t2 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
counter++;
}
});
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(counter);
}
}
Run Code Online (Sandbox Code Playgroud)
结果始终是2000,但是我不知道为什么。任何运行方法都不同步,所以为什么它总是给我相同的结果。
如果我写:
t1.start();
t1.join();
System.out.println(counter);
t2.start();
System.out.println(counter);
Run Code Online (Sandbox Code Playgroud)
然后我得到结果:1000,1000。为什么总是等于1000?
我正在尝试对负责从文本文件中检索数据的单元测试方法进行测试。
使用该方法的类如下所示:
package contentfile;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class ContentFileRetrieverService implements ContentFileRetriever {
@Override
public String[] getContentFile(String pathName) {
Stream<String> contentFileStream;
try {
contentFileStream = Files.lines(Paths.get(pathName));
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
return contentFileStream.toArray(String[]::new);
}
}
Run Code Online (Sandbox Code Playgroud)
这是测试的样子:
package contentfile;
import org.junit.Rule;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.*;
class ContentFileRetrieverServiceTest {
private ContentFileRetrieverService contentFileRetrieverService;
// @Rule
// TemporaryFiles temporaryFiles = new TemporaryFiles();
@Test
void getContentFile() {
String pathFile = "tekst.txt";
String[] testedContent = contentFileRetrieverService.getContentFile(pathFile); …Run Code Online (Sandbox Code Playgroud) java ×6
unit-testing ×3
localdate ×2
spring ×2
file ×1
json ×1
junit ×1
python ×1
spring-boot ×1
spring-mvc ×1