没有为响应类型找到合适的HttpMessageConverter

使用spring,使用以下代码: List<HttpMessageConverter> messageConverters = restTemplate.getMessageConverters(); for(HttpMessageConverter httpMessageConverter : messageConverters){ System.out.println(httpMessageConverter); } ResponseEntity productList = restTemplate.getForEntity(productDataUrl,ProductList.class); 我明白了 org.springframework.http.converter.ByteArrayHttpMessageConverter@34649ee4 org.springframework.http.converter.StringHttpMessageConverter@39fba59b org.springframework.http.converter.ResourceHttpMessageConverter@383580da org.springframework.http.converter.xml.SourceHttpMessageConverter@409e850a org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@673074aa org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@1e3b79d3 org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@52bb1b26 org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.mycopmany.ProductList] and content type [text/html;charset=UTF-8] pojo的片段: @XmlRootElement(name=”TheProductList”) public class ProductList { @XmlElement(required = true, name = “date”) private LocalDate […]

Java未选中/已检查exception澄清

我一直在阅读关于未经检查和已检查的问题,没有一个在线资源真正清楚这些差异以及何时使用这两者。 根据我的理解,它们都会在运行时抛出,它们都代表超出逻辑预期范围的程序状态,但必须明确捕获已检查的exception,而未经检查的exception则不会。 我的问题是,假设为了论证我有一个方法来划分两个数字 double divide(double numerator, double denominator) { return numerator / denominator; } 以及需要在某处使用divison的方法 void foo() { double a = divide(b, c); } 谁负责检查分母为零的情况,是否应该检查或取消检查exception(忽略Java内置的分区检查)? 那么,除法方法是否被声明为或 double divide(double numerator, double denominator) throws DivideByZeroException { if(denominator == 0) throw DivideByZeroException else … } void foo() { try{ double a = divide(b, c); } catch(DivideByZeroException e) {} […]

手编辑一个jar子来改变包装名称

我有一个来自外部源的jar文件。 jar中的所有类都在com.xyz包中。 我想将所有类移到com.xyzold包中。 这是简单的解压缩jar,将xzy文件夹重命名为xyzold,并重新压缩它,还是我还需要修改每个类文件? 这是我的解决方案,使用Jar Jar Links java -jar jarjar-1.4.jar process rules.txt google-collections-1.0.jar google-collections-old-1.0.jar 这是我的rules.txt文件的内容: rule com.google.** com.googleold.@1

请求的资源上不存在Access-Control-Allow-Origin标头

我想访问来自相同域但具有不同端口号的信息,为了允许这一点,我将Access-Control-Allow-Origin响应头添加Access-Control-Allow-Origin 。 Servlet代码:(发布于www.example.com:PORT_NUMBER) String json = new Gson().toJson(list); response.setContentType(“application/json”); response.setCharacterEncoding(“UTF-8”); response.setHeader(“Access-Control-Allow-Origin”, “*”);//cross domain request/CORS response.getWriter().write(json); jQuery代码:(发布在www.example.com上) $.post(‘http://www.example.com:PORT_NUMBER/MYSERVLET’,{MyParam: ‘value’}).done(function(data) { alert(data); }); 好几次我收到这个错误(在控制台中): XMLHttpRequest cannot load ‘http://www.example.com:PORT_NUMBER/MYSERVLET’ No ‘Access-Control-Allow-Origin’ header is present on the requested resource. 这个错误通常在$.post执行时第一次出现。 第二次它允许。 我的问题是servlet或jQuery代码中是否缺少? 任何建议将不胜感激。 UPDATE1 我变了: response.setHeader(“Access-Control-Allow-Origin”, “*”); 至: response.setHeader(“Access-Control-Allow-Origin”, “http://www.example.com”); 然后我在控制台中收到此错误: XMLHttpRequest cannot load http://www.example.com:PORT_NUMBER/MyServletName The ‘Access-Control-Allow-Origin’ whitelists only […]

使用Enum的序数值来索引Java中的数组是不好的做法吗?

我有两个数组:Walls和Neighbors。 public boolean[] walls = new boolean[4]; public Cell[] neighbors = new Cell[4]; 我有一个枚举: enum Dir { North, South, East, West } 现在,我希望能够按照他们的方向访问墙壁或邻居,所以我不必传递一堆魔法索引。 但是,当我阅读Enum.ordinal()的文档时,它说程序员几乎没有使用这种方法,这让我认为它不应该以这种方式使用。 我想做的事情如下: List availableDirections = new ArrayList(); for(Dir direction : Dir.values()) if (!Neighbors[direction.ordinal()].Visited) availableDirections.add(direction); 甚至: return Neighbors[Dir.North.ordinal()]; 我应该恢复使用设置为索引值的NORTH,SOUTH,EAST,WEST的静态常量还是使用Enum的序数方法?

什么时候被称为spring beans destroy-method?

我在bean的“destroy-method”中放了一个sysout语句。 当我运行示例代码时,sysout没有得到输出。 这是否意味着破坏方法没有被调用? 测试类: package spring.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class InitTest { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext(“InitTestContext.xml”); InitTestBean bean = (InitTestBean)ctx.getBean(“InitTestBean”); bean.display(); } } 豆 package spring.test; public class InitTestBean { private String prop1; private String prop2; public InitTestBean(String prop1, String prop2) { System.out.println(“Instantiating InitTestBean”); this.prop1 = […]

LDAP:如何使用连接详细信息validation用户身份

我无法使用LDAP对用户进行身份validation。 我有以下细节: URL=ldap://10.10.10.10:389 LDAP BASE:DC=lab2,DC=ins LDAP Bind Account: CN=Ldap Bind,OU=Service Accounts,OU=TECH,DC=lab2,DC=ins LDAP Bind Account Pw: secret 我可以使用上面的详细信息搜索sAMAccountName值,但是如何使用用户名和密码validation用户? 如果您按照我之前的问题进行操作,那么您将了解到,我已成功连接到LDAP服务器但无法对其进行身份validation。 用户进行身份validation: user: someusername password: somepwd 我无法使用’somepwd’连接到LDAP服务器,我应该如何使用someusername 。 我能够将给定用户搜索为sAMAccountName 。

在JAX-RS中使用@ Context,@ Provider和ContextResolver

我刚刚熟悉使用JAX-RS在Java中实现REST Web服务,我遇到了以下问题。 我的一个资源类需要访问存储后端,后端在StorageEngine接口后面被抽象出来。 我想将当前的StorageEngine实例注入到为REST请求提供服务的资源类中,我认为这样做的好方法是使用@Context注释和适当的@Context类。 这是我到目前为止: 在MyResource.java : class MyResource { @Context StorageEngine storage; […] } 在StorageEngineProvider.java : @Provider class StorageEngineProvider implements ContextResolver { private StorageEngine storage = new InMemoryStorageEngine(); public StorageEngine getContext(Class type) { if (type.equals(StorageEngine.class)) return storage; return null; } } 我正在使用com.sun.jersey.api.core.PackagesResourceConfig自动发现提供程序和资源类,并根据日志,它很好地获取StorageEngineProvider类(故意留下时间戳和不必要的东西): INFO: Root resource classes found: class MyResource INFO: Provider classes found: class […]

如何使用JSoup发布文件?

我使用JSoup使用以下代码发布值: Document document = Jsoup.connect(“http://www……com/….php”) .data(“user”,”user”,”password”,”12345″,”email”,”info@tutorialswindow.com”) .method(Method.POST) .execute() .parse(); 现在我也想提交一份文件。 就像带有文件字段的表单一样。 这可能吗 ? 如果比怎么样?

为什么Hashtable不允许空键或值?

正如JDK文档中所指定的,Hashtable不允许使用null键或值。 HashMap允许一个空键和任意数量的空值。 为什么是这样?