具有MongoDB POST和GET记录的Spring控制器(Spring controller with MongoDB POST and GET records)
当我想在我的数据库中发布并检索它时,我需要帮助了解我应该如何以及为什么要使用控制器。 当我只使用Post.java和PostRepository.java时,它似乎在我插入数据以及在我的“/ posts”路径中检索整个数据库时起作用。 在文件里面我有一个包含所有条目的posts数组。
Post.java
public class Post { @Id private long id; private String content; private String time; private String gender; private String age; // Constructors public Post() { } public Post(long id, String content, String time, String gender, String age) { this.id = id; this.content = content; this.time = time; this.gender = gender; this.age = age; } // Getters public String getContent() { return content; } public long getId() { return id; } public String getTime() { return time; } public String getGender() { return gender; } public String getAge() { return age; }
PostRepository.java
@RepositoryRestResource(collectionResourceRel = "posts", path = "posts") public interface PostRepository extends MongoRepository<Post, String> { List<Post> findPostByContent(@Param("content") String content); }
PostController.java
@RestController public class PostController { private final AtomicLong counter = new AtomicLong(); //Insert post in flow @RequestMapping(value="/posts", method = RequestMethod.POST) public Post postInsert(@RequestBody Post post) { return new Post(counter.incrementAndGet(), post.getContent(), post.getTime(), post.getGender(), post.getAge()); } //Get post in flow @RequestMapping(value="/posts", method = RequestMethod.GET) public Post getPosts() { return null; //here I want to return all posts made using postInsert above. } }
当我使用我的控制器时,我可以发布数据但它没有保存在json文件中,所以当我重新启动我的应用程序时,我再次从id:1开始。 但是,如果没有控制器,则会保存数据。 为什么会这样? 如何安排以便控制器保存数据? 我知道这可能是一个愚蠢的问题,但我不知道该怎么办。
I need help understanding how and why I should use controllers when I want to post something in my DB and retrieving it. When I only use Post.java and PostRepository.java, it seems to work when I insert data as well as retrieving the entire database in my "/posts" path. Inside the file I have a posts array containing all the entries.
Post.java
public class Post { @Id private long id; private String content; private String time; private String gender; private String age; // Constructors public Post() { } public Post(long id, String content, String time, String gender, String age) { this.id = id; this.content = content; this.time = time; this.gender = gender; this.age = age; } // Getters public String getContent() { return content; } public long getId() { return id; } public String getTime() { return time; } public String getGender() { return gender; } public String getAge() { return age; }
PostRepository.java
@RepositoryRestResource(collectionResourceRel = "posts", path = "posts") public interface PostRepository extends MongoRepository<Post, String> { List<Post> findPostByContent(@Param("content") String content); }
PostController.java
@RestController public class PostController { private final AtomicLong counter = new AtomicLong(); //Insert post in flow @RequestMapping(value="/posts", method = RequestMethod.POST) public Post postInsert(@RequestBody Post post) { return new Post(counter.incrementAndGet(), post.getContent(), post.getTime(), post.getGender(), post.getAge()); } //Get post in flow @RequestMapping(value="/posts", method = RequestMethod.GET) public Post getPosts() { return null; //here I want to return all posts made using postInsert above. } }
When I use my controllers I can post data but it is not saved in a json file so when I restart my application I start from id: 1 again. However, without the controller the data is saved. Why is this happening? How can I arrange so that with controllers the data is saved as well? I know this might be a stupid question but I don't know what to do.
原文:https://stackoverflow.com/questions/36691785
最满意答案
比较两个浮点值的常用方法是将它们相互减去,得到结果的绝对值,并将其与epsilon值进行比较。
在你的情况下它可能是这样的
bool compare(double value1, double value2, quint8 precision) { return std::abs(value1 - value2) < std::pow(10, -precision); }
对于例如
6
的精度,则std::pow(10, -precision)
应该等于0.000001
(这是epsilon ),并且如果两个值之间的差小于它们被认为是相等的。The usual way to compare two floating point values is to subtract them from each other, get the absolute value of the result, and compare it to an epsilon value.
In your case it could be something like
bool compare(double value1, double value2, quint8 precision) { return std::abs(value1 - value2) < std::pow(10, -precision); }
For a precision of e.g.
6
thenstd::pow(10, -precision)
should equal0.000001
(this is the epsilon), and if the difference between the two values is smaller than that they are considered equal.
相关问答
更多-
尝试 printf("%.0lf\n",phoneNum); 你也可以选择 long long phoneNum; phoneNum = strtoll(buffer,NULL,0); printf("%lld\n",phoneNum); 代替。 不过,根据系统的不同,您可能需要其他功能进行转换(我认为它是windows的_strtoui64 )。 try printf("%.0lf\n",phoneNum); you may also prefer long long phoneNum; phone ...
-
您可以使用另一种输出方法,如下所示: System.out.format("%f%5f\n",ounce, gramsFormated); 你可以决定你的宽度。 在这个例子中它是5。 You can use a another output method like this: System.out.format("%f%5f\n",ounce, gramsFormated); You can decide how you want your width. In ...
-
将Double值格式化为2个小数位的最佳方式[重复](Best way to Format a Double value to 2 Decimal places [duplicate])[2022-06-11]
不,没有更好的方法。 其实你的模式有错误。 你想要的是: DecimalFormat df = new DecimalFormat("#.00"); 注意"00" ,意思是两位小数。 如果您使用"#.##" ( #表示“可选”数字),则会拖尾为零 - 即new DecimalFormat("#.##").format(3.0d); 打印只是"3" ,而不是"3.00" 。 No, there is no better way. Actually you have an error in your pat ... -
限制小数点后加小到3位(Limiting double to 3 decimal places)[2023-11-26]
双数没有小数位数 - 它们不是基于开头的十进制数字。 您可以获得“当截断为三位十进制数时最接近的当前值的双倍”,但它仍然不会完全相同。 你最好使用decimal 。 话虽如此,如果这只是舍入发生的一个问题,你可以使用Math.Truncate(value * 1000) / 1000; 这可以做你想要的 (你根本就不希望四舍五入 )它仍然是潜在的“狡猾”,结果仍然不会只有三位小数。 但是,如果您使用十进制值做同样的事情,它将会起作用: decimal m = 12.878999m; m = Math.Tru ... -
听起来你想要实际值的任意精度(而不仅仅是输出)。 double不会给你那个。 但BigDecimal确实如此。 它的BigDecimal(String)构造函数从BigDecimal(String)设置值和比例(小数点右边的位数),因此: BigDecimal d = new BigDecimal("1.10900"); BigDecimal然后为您提供各种数学运算以保持在该范围内,具有各种舍入选项。 如果在某些时候你需要获得BigDecimal的double值,你可以使用它的doubleValue方法 ...
-
我认为这个功能应该这样做: Public Function GetDecimalPlaces(ByVal Value As Double, ByVal Culture As System.Globalization.CultureInfo) As Integer Dim val As String = String.Empty Dim valSplitted As String() = Nothing Try val = Va ...
-
比较两个浮点值的常用方法是将它们相互减去,得到结果的绝对值,并将其与epsilon值进行比较。 在你的情况下它可能是这样的 bool compare(double value1, double value2, quint8 precision) { return std::abs(value1 - value2) < std::pow(10, -precision); } 对于例如6的精度,则std::pow(10, -precision)应该等于0.000001 (这是epsilon ),并且如 ...
-
您可以格式化double,指定小数位数,如下所示: double d = 1.234567; DecimalFormat df = new DecimalFormat("#.###"); System.out.print(df.format(d)); You can format a double, specifying the number of decimal places as follows: double d = 1.234567; DecimalForm ...
-
显示一些小数位C(Show some decimal places C)[2020-07-08]
您可以创建一个函数来计算所需的小数位数。 要做到这一点,取小数部分,并乘以10,直到它变成一个整数。 int required_decimal_places(double x) { int counter = 0; x -= floor(x); while (x != floor(x)) { x *= 10; ++counter; } return counter; } 然后,输出您的号码所需的小数位数: printf(" ... -
双2小数位[重复](Double 2 Decimal Places [duplicate])[2022-06-20]
使用格式字符串: sw.WriteLine("( {0:0.00}, {1:0.00} )", xValue, yValue); 有关文档,请查看标准数字格式字符串 。 String.Format和TextWriter.WriteLine公开相同的格式选项。 Use a format string: sw.WriteLine("( {0:0.00}, {1:0.00} )", xValue, yValue); For documentation, look at Standard Numeric For ...