首页 \ 问答 \ 具有MongoDB POST和GET记录的Spring控制器(Spring controller with MongoDB POST and GET records)

具有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
更新时间:2023-05-04 08:05

最满意答案

比较两个浮点值的常用方法是将它们相互减去,得到结果的绝对值,并将其与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 then std::pow(10, -precision) should equal 0.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 ...
  • 不,没有更好的方法。 其实你的模式有错误。 你想要的是: 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 ...
  • 双数没有小数位数 - 它们不是基于开头的十进制数字。 您可以获得“当截断为三位十进制数时最接近的当前值的双倍”,但它仍然不会完全相同。 你最好使用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 ...
  • 您可以创建一个函数来计算所需的小数位数。 要做到这一点,取小数部分,并乘以10,直到它变成一个整数。 int required_decimal_places(double x) { int counter = 0; x -= floor(x); while (x != floor(x)) { x *= 10; ++counter; } return counter; } 然后,输出您的号码所需的小数位数: printf(" ...
  • 使用格式字符串: 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 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。