我意识到这是一个鸡和蛋的问题,并且不可能准确地解决呈现页面所花费的时间(或响应的大小)并将该数字插入页面本身而不影响任何一个度量.不过,我正在寻找一种方法来将一个数字部分插入JSF/Facelets/Seam应用程序的页面中.
例如,在某个地方的.jsf页面的底部:
<!-- page size: 10.3Kb -->
<!-- render time: 0.2s -->
Run Code Online (Sandbox Code Playgroud)
我遇到过JSFUnit的JSFTimer,非常方便.但是,相位侦听器方法不允许将RENDER_RESPONSE阶段的结果插入到页面中.不知道如何访问目前为止编码的响应的大小.
在RENDER_RESPONSE结束时或之后是否有一种快速而肮脏的方式来连接某种后处理事件,并将这两个数字注入到即将呈现的页面中?接近这个的一种方法可能是通过servlet过滤器,但我正在寻找更简单的东西; 也许是Seam或Facelets的一招
谢谢,
-A
如果客户端将不支持的媒体类型的数据发送到HTTP服务器,则服务器将回答状态为" 415不支持的媒体类型 ".但是如何告诉客户端支持哪些媒体类型?是否有标准或至少推荐的方法?或者它只是作为文本写入响应主体?
rest http httpresponse http-status-codes content-negotiation
我正在尝试通过创建一个小型RESTful服务来学习球衣.我想出于特定原因使用Filters(就像我想使用ContainerResponseFilter for CORS头来允许跨域请求).但是,我只是无法让这些过滤器拦截我的电话.我已经看到了这个问题的所有帖子,他们中的大多数都说要注册注释提供者或web.xml.我已经尝试在web.xml中注册文件以及为@Provider容器提供注释
这是我的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html#d4e194 -->
<web-app version="2.5" 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_2_5.xsd">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring/config/BeanLocations.xml</param-value>
</context-param>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.rest.example</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.rest.example.cors</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
<param-value>com.rest.example.CORSFilter</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>com.rest.example.RequestFilter</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webresources/*</url-pattern>
</servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
这是我的过滤器:
package com.rest.example.cors;
import javax.ws.rs.ext.Provider;
import com.sun.jersey.spi.container.ContainerRequest;
import …Run Code Online (Sandbox Code Playgroud) 我正在研究遗留的django项目,在那里有一个类定义如下;
from django.http import HttpResponse
class Response(HttpResponse):
def __init__(self, template='', calling_context='' status=None):
self.template = template
self.calling_context = calling_context
HttpResponse.__init__(self, get_template(template).render(calling_context), status)
Run Code Online (Sandbox Code Playgroud)
并且此类在视图中使用如下
def some_view(request):
#do some stuff
return Response('some_template.html', RequestContext(request, {'some keys': 'some values'}))
Run Code Online (Sandbox Code Playgroud)
这个类主要是为了让他们可以在单元测试中使用它来执行断言.他们没有使用django.test.Client来测试视图,而是创建了一个模拟请求并将其传递给视图(调用视图)作为可调用的)在测试中如下
def test_for_some_view(self):
mock_request = create_a_mock_request()
#call the view, as a function
response = some_view(mock_request) #returns an instance of the response class above
self.assertEquals('some_template.html', response.template)
self.assertEquals({}, response.context)
Run Code Online (Sandbox Code Playgroud)
问题是在测试套件的一半(相当大的测试套件),一些测试在执行时开始爆炸
return Response('some_template.html', RequestContext(request, {'some keys': 'some values'}))
Run Code Online (Sandbox Code Playgroud)
并且堆栈跟踪是
self.template = template
AttributeError: can't set attribute
Run Code Online (Sandbox Code Playgroud)
完整的堆栈跟踪看起来像
======================================================================
ERROR: …Run Code Online (Sandbox Code Playgroud) 在尝试将json字符串解析为android时,会传递HTML值.在一天之前一切都运行良好,突然我的应用程序在尝试使用php文件的帮助获取数据库时开始崩溃.
当我检查注意到html值..请参阅logcat
08-10 01:09:55.814: E/result(6744): <html><body><h2>Checking your browser..<h2><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("7965e114a1dccaf35af3756261f75ad8");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/";location.href="http://realroom.byethost24.com/medical/stokist.php?ckattempt=1";</script></body></html>
08-10 01:09:55.814: E/JSON Parser(6744): Error parsing data org.json.JSONException: Value <html><body><h2>Checking of type java.lang.String cannot be converted to JSONObject
08-10 01:09:55.816: E/AndroidRuntime(6744): FATAL EXCEPTION: AsyncTask #1
08-10 01:09:55.816: E/AndroidRuntime(6744): Process: com.example.medionline, PID: 6744
08-10 01:09:55.816: E/AndroidRuntime(6744): java.lang.RuntimeException: An error occured while executing doInBackground()
08-10 01:09:55.816: E/AndroidRuntime(6744): at android.os.AsyncTask$3.done(AsyncTask.java:304)
08-10 01:09:55.816: E/AndroidRuntime(6744): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
08-10 01:09:55.816: E/AndroidRuntime(6744): at java.util.concurrent.FutureTask.setException(FutureTask.java:222) …Run Code Online (Sandbox Code Playgroud) 我正在尝试使一些微服务更具弹性,并且重试某些类型的HTTP请求将有助于此.
重试超时会给客户带来非常缓慢的体验,因此我不打算在这种情况下重试.重试400s无济于事,因为错误的请求在几毫秒后仍然是一个错误的请求.
我想还有其他原因不能重试一些其他类型的错误,但是哪些错误以及为什么?
我正在寻找一个库,用Arduino解析Web HTTP响应,例如,从内容中分离HTTP Headers.
阅读各种草图,解析数据流的最先进工具是TextFinder.
您是否了解任何其他可以帮助解析HTTP响应的库?
我想要一个视图来返回图像数据.所以有些东西
return HttpResponse(image_data, mimetype=”image/png”)
Run Code Online (Sandbox Code Playgroud)
我知道我可以做一个file.read()来获取图像数据,但因为图像很小(比如1x1 px)我想把它存储为一个字符串对象(或者我可以复制并粘贴到我的代码中的任何对象).这样,每次查看视图时,我都会自行保存磁盘查找.
我该怎么做?我确信这很简单,我只是不确定用于搜索的术语.
ps我知道一个人通常不会用Django以这种方式提供图像.
我已经看到一些非常难看的代码来自人们编写自己的方法将HttpResponse转换为字符串以便稍后使用,看起来像这样:
httppost.setEntity(new UrlEncodedFormEntity(valuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF8"),8);
StringBuilder sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
String result = sb.toString();
Run Code Online (Sandbox Code Playgroud)
这不仅是一个混乱,但它真的很难看,而且我常常无法分辨代码发生了什么,因为这个混乱在它之前.有没有更好的方法来做到这一点?
我有大约40个具有类似基本响应结构的API,如下所示:
{
"lastAccessed": "2015-30-08:14:21:45T",
"createdOn": "2015-30-07:09:04:10T",
"lastModified": "2015-30-08:14:21:45T",
"isReadOnly": "false",
"usersAllowed" : ["Tim", "Matt", "Christine"];
"noOfEntries": 1,
"object": [
"ObjectA": {
//here object A has its own model
}
]
}
Run Code Online (Sandbox Code Playgroud)
所以我有一个基类响应类,它采用类型T的泛型,如下所示:
public class Response<T> {
@ApiModelProperty(value="Last time accessed")
private String lastAccessed;
@ApiModelProperty(value="Time when Created ")
private String createdOn;
private String lastModified;
@ApiModelProperty(value="Created on")
private boolean isReadOnly;
@ApiModelProperty(value="Users that has access to the object.")
private List<String> usersAllowed;
private int noOfEntries;
private T object;
//getters and setters
}
Run Code Online (Sandbox Code Playgroud)
因此对于API A,它返回具有自己字段的Object类型,我将返回Response作为控制器中的API响应:
public …Run Code Online (Sandbox Code Playgroud)