小编mar*_*tin的帖子

Spring:返回@ResponseBody"ResponseEntity <List <JSONObject >>"

在控制器中我创建了json数组.如果我退货List<JSONObject>就可以了:

@RequestMapping(value="", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<JSONObject> getAll() {
    List<Entity> entityList = entityManager.findAll();

    List<JSONObject> entities = new ArrayList<JSONObject>();
    for (Entity n : entityList) {
        JSONObject entity = new JSONObject();
        entity.put("id", n.getId());
        entity.put("address", n.getAddress());
        entities.add(entity);
    }
    return entities;
}
Run Code Online (Sandbox Code Playgroud)

但我需要返回JSON数组和HTTP状态代码:

@RequestMapping(value="", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody ResponseEntity<List<JSONObject>> getAll() {
    List<Entity> entityList = entityManager.findAll();

    List<JSONObject> entities = new ArrayList<JSONObject>();
    for (Entity n : entityList) {
        JSONObject Entity = new JSONObject();
        entity.put("id", n.getId());
        entity.put("address", n.getAddress());
        entities.add(entity);
    }
    return new ResponseEntity<JSONObject>(entities, HttpStatus.OK); // …
Run Code Online (Sandbox Code Playgroud)

java rest spring json http-status-codes

32
推荐指数
3
解决办法
12万
查看次数

Spring MVC:通用DAO和服务类

我正在Spring MVC中编写Web.我使用Generic DAO编写了所有DAO.现在我想重写我的服务类.我怎么写"通用服务"?

有我的DAO:

/* ################################# DAO ################################ */
package net.example.com.dao;

import java.util.List;

public interface GenericDao<T> {       
        public T findById(int id);     
        public List<T> findAll();      
        public void update(T entity);  
        public void save(T entity);    
        public void delete(T entity);
}

/* ------------------------------------------------------ */

package net.example.com.dao;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

@Scope("prototype")
public abstract class GenericHibernateDaoImpl<T extends Serializable> implements GenericDao<T> {

        private Class<T> clazz;

        @Autowired
        private SessionFactory sessionFactory;

        public final void setClazz(Class<T> clazzToSet) {
                this.clazz = clazzToSet; …
Run Code Online (Sandbox Code Playgroud)

java generics spring dao genericdao

7
推荐指数
1
解决办法
1万
查看次数

最短的哈希?MD5/SHA.第一个字符,git

我需要哈希函数.用户将这些哈希写入计算机,因此哈希应该很短.我将在数据库中拥有大约5 000 000条记录.每个都必须有自己的哈希.我想有独特的哈希.但如果一些记录有相同的哈希值,我可以接受.独特更好.

MD2对我来说是安全的,但哈希很长:"8350e5a3e24c153df2275c9f80692773" - 32个字符.如果你必须在keybord上写10个MD2哈希你不开心...

Git每次提交都使用SHA1(40个字符).但在输出中只显示前7个字符:

$ git log
commit e2cfc89fae5b43594b2c649fd4c05bcc6d2d12ac
...
commit 56a8b4c50d4269dc3f88727472933fd81231f63b
...
commit ce2e9ddbe896b9592abbd5fcb6604b181809d523
...
commit 498c49833516ea33b6a40697634ea6e3cfd62328
...
commit b7d78aea415e64d8d441f9747fe6d5d48fe54ee5

$ git log --oneline | head -n 5
e2cfc89 commnit message...
56a8b4c commnit message...
ce2e9dd commnit message...
498c498 commnit message...
b7d78ae commnit message...
Run Code Online (Sandbox Code Playgroud)

它是如何安全/独特的?如果我将使用例如来自MD5/SHA-1/SHA-256的前5个或10个字符,它是否足够安全?

谢谢.

git hash md5 sha

6
推荐指数
2
解决办法
6980
查看次数

Java MODBUS RTU主代码示例代码

我需要用Java支持03 - Read Holding Registers和编写Modbus RTU主应用程序16 - Write Multiple Registers.

我发现了三个Java库:jamod,j2mod,modbus4j.我尝试了所有这些库(我花了大约4个小时)但它仍然无法正常工作.

您是否知道任何分步教程或示例代码?

我正在使用USB-> RS-485转换器.如果我在QModBus中测试,它运行良好.

谢谢.

import java.io.File;
import com.serotonin.io.serial.SerialParameters;
import com.serotonin.modbus4j.ModbusFactory;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.code.DataType;
import com.serotonin.modbus4j.code.RegisterRange;
import com.serotonin.modbus4j.exception.ModbusInitException;

public class Modbus4JTest {

   public static void main(String[] args) throws Exception {  
        ModbusFactory factory = new ModbusFactory();
        SerialParameters params = new SerialParameters();
          params.setCommPortId("/dev/ttyUSB1");
          params.setBaudRate(9600);
          params.setDataBits(8);
          params.setStopBits(1);
          params.setParity(0);
        ModbusMaster master = factory.createRtuMaster(params);
        master.setTimeout(2000);
        master.setRetries(0);
        long start = System.currentTimeMillis();

        // Don't start if …
Run Code Online (Sandbox Code Playgroud)

java serial-port modbus rxtx

6
推荐指数
0
解决办法
1万
查看次数

HTTP 测试服务器:将所有客户端请求打印到标准输出

我需要简单的 Web 服务器来调试我的应用程序(Arduino Web 客户端、curl 等)。

我的想法是什么:

我运行命令

curl -X POST -H "Content-Type: application/json" \
  -H "Accept: application/json" -d '{"address":"192.168.200.3", \
  "title":"Abc" }' http://SERVER/xyz
Run Code Online (Sandbox Code Playgroud)

测试运行在http://SERVER:80. 此网络服务器将数据 + 所有 http 标头写入标准终端输出。

它非常适合使用以太网屏蔽测试 Arduino。

是否有任何现有产品(适用于 Linux)?我可以用 Java 编写它,但我不想重新发明轮子...

java webserver curl http arduino

5
推荐指数
1
解决办法
2552
查看次数

Python在内存中解压gzip数据,无需文件

我已经从 HTTP 回复中压缩了数据。我有以下代码:

def gzipDecode(self, content):
    import StringIO
    import gzip

    outFilePath = 'test'

    compressedFile = StringIO.StringIO(content)
    decompressedFile = gzip.GzipFile(fileobj=compressedFile)
    with open(outFilePath, 'w') as outfile:
        outfile.write(decompressedFile.read())

    data = ''
    with open(outFilePath, 'r') as myfile:
        data=myfile.read().replace('\n', '')

    return data
Run Code Online (Sandbox Code Playgroud)

它解压缩输入的 gzip 压缩内容并返回字符串(http 回复是 gzip 压缩的 json)。- 有用。

但我需要它而不创建测试文件 - 全部在内存中。

我将其修改为:

def gzipDecode(self, content):
    import StringIO
    from io import BytesIO
    import gzip

    outFile = StringIO.StringIO()

    compressedFile = StringIO.StringIO(content)
    decompressedFile = gzip.GzipFile(fileobj=compressedFile)

    outFile.write(decompressedFile.read())
    outFile.flush()

    data = outFile.read().replace('\n', '')
    print "_" + …
Run Code Online (Sandbox Code Playgroud)

python compression json gzip stringio

5
推荐指数
1
解决办法
3150
查看次数

我无法将目录添加到 .gitignore

我无法将“taget”目录添加到“.gitignore”文件中。我做了坏事:-(

martin@martin:~/git/gitRepo$ tree -L 3 -a
.
??? .git
  ...
??? .gitignore
??? MyProject
    ??? .classpath
    ??? pom.xml
    ??? .project
    ??? .settings
  ...
    ??? .springBeans
    ??? src
    ?   ??? main
    ?   ??? test
    ??? target
        ??? classes
        ??? m2e-wtp
        ??? test-classes

269 directories, 32 files
martin@martin:~/git/gitRepo$ 
martin@martin:~/git/gitRepo$ cat .gitignore 
*.*~
*.class
*.jar
*.war
*.ear
MyProject/.classpath
MyProject/.project
MyProject/.settings/
MyProject/target/
/MyProject/target/
target
martin@martin:~/git/gitRepo$ 
martin@martin:~/git/gitRepo$ git status 
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to …
Run Code Online (Sandbox Code Playgroud)

git ignore gitignore

3
推荐指数
1
解决办法
3604
查看次数

Pycharm:Python Qt代码代码完成

我是Python Qt的初学者。

我可以使用Qt Designer创建简单的东西。

在此处输入图片说明

我需要的-用户单击按钮后,应用程序将文本从编辑复制到标签。

我有example.ui来自Qt Designer的文件:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>308</width>
    <height>143</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>20</y>
      <width>121</width>
      <height>17</height>
     </rect>
    </property>
    <property name="text">
     <string>Enter name</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="lineEdit">
    <property name="geometry">
     <rect>
      <x>100</x>
      <y>20</y>
      <width>113</width>
      <height>27</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>80</x>
      <y>60</y>
      <width>85</width>
      <height>27</height>
     </rect>
    </property>
    <property …
Run Code Online (Sandbox Code Playgroud)

python qt pyqt4 code-completion pycharm

1
推荐指数
1
解决办法
2416
查看次数

Spring+Hibernate:读/写不同的数据源和数千个mysql数据库

我需要使用单独的读取器/写入器 MySQL 服务器。一个写入器和一个(或多个)只读副本。

做这个的最好方式是什么?

在此处输入图片说明

我找到了很多例子:


http://www.dragishak.com/?p=307

使用特殊的 JDBC 驱动程序: com.mysql.jdbc.ReplicationDriver

用法是:

@Transactional(readOnly=true)
@ReadOnlyConnection
public Result serviceMethod(…) {
   …
}
Run Code Online (Sandbox Code Playgroud)

/sf/answers/2321891561/

使用弹簧AbstractRoutingDatasource

用法:

@Transactional(readOnly = true)
public Page<BookDTO> getBooks(Pageable p) {
  try{
    DbContextHolder.setDbType(DbType.REPLICA1);   // <----- set ThreadLocal DataSource lookup key
Run Code Online (Sandbox Code Playgroud)

在每种方法中,我都需要设置 DbType。

是否可以自动将“读取查询”发送到副本服务器并将“写入查询”(插入/更新)发送到主服务器?


第二个问题:

我想每个用户有一个 mysql 数据库(非常大)。我预计大约有 2000 名用户。所以我不能为每个用户*读者+作者定义数据源。

例子:

spring.ds_items.driverClassName=com.mysql.jdbc.Driver
spring.ds_items.url=jdbc:mysql://mysql-master/user1
spring.ds_items.username=root 
spring.ds_items.password=12345

spring.ds_items.driverClassName=com.mysql.jdbc.Driver
spring.ds_items.url=jdbc:mysql://mysql-replica1/user1
spring.ds_items.username=root 
spring.ds_items.password=12345

spring.ds_items.driverClassName=com.mysql.jdbc.Driver
spring.ds_items.url=jdbc:mysql://mysql-master/user2
spring.ds_items.username=root 
spring.ds_items.password=12345

spring.ds_items.driverClassName=com.mysql.jdbc.Driver
spring.ds_items.url=jdbc:mysql://mysql-replica1/user2
spring.ds_items.username=root 
spring.ds_items.password=12345
Run Code Online (Sandbox Code Playgroud)

我想要一个“主 mysql 数据库”,其中包含如下表:

user   db_name
--------------
test1  db_test1
test2  db_test2
Run Code Online (Sandbox Code Playgroud)

如果我需要用户的一些数据,test2我会查看“主数据库”并获取 …

java mysql spring hibernate database-replication

0
推荐指数
1
解决办法
3607
查看次数