我一直在忙着JSON一段时间,只是把它作为文本推出它并没有伤害任何人(我知道),但我想开始正确地做事.
我见过这样的JSON内容类型很多所谓的"标准":
application/json
application/x-javascript
text/javascript
text/x-javascript
text/x-json
Run Code Online (Sandbox Code Playgroud)
但哪个是正确的,还是最好的?我认为它们之间存在安全性和浏览器支持问题.
我知道有一个类似的问题,如果REST API返回JSON,那么MIME类型是什么?,但我想要一个稍微有针对性的答案.
我是Web应用程序和Servlet的新手,我有以下问题:
每当我在servlet中打印一些东西并通过webbrowser调用它时,它就会返回一个包含该文本的新页面.有没有办法使用Ajax在当前页面中打印文本?
在进入细节之前,我知道Stackoverflow上有很多对话和相关问题.所有这些都以不同的方式帮助我,所以我认为我把我的发现全部放在一起作为一个单独的有组织的常见问题解答来总结我的发现.
当然你知道这些,但我只是把它们写成快速回顾.如果我遗漏了什么,请随时编辑.
当您愿意将对象发送到Web服务或服务器端应用程序时,将使用发布请求.
是将对象从Web浏览器传递到服务器端应用程序的过程.可以使用jQuery Ajax调用或Curl post请求.
最流行的那些日子是JSON和XML.由于XML标记的性质,序列化的xml对象的大小相对较大,因此XML变得越来越不受欢迎.在本FAQ中,主要关注的是JSON2序列化.
Spring框架及其强大的注释使得以有效的方式公开Web服务成为可能.Spring中有很多不同的库.我们关注的是Spring web MVC.
这些是您可以用来在客户端发布帖子请求的工具.即使您计划使用JQuery ajax调用,我建议您使用Curl进行调试,因为它会在发出请求后为您提供详细的响应.
如果您的Web服务不依赖于Java EE模型,则必须使用@RequestBody.如果您正在使用模型并且您的JSON对象已添加到模型中,则可以通过@ModelAttribute访问该对象.仅在您的请求是GET请求或GET和POST请求组合的情况下,您将需要使用@RequestParam/@ PathVariable.
正如您从名称中看到的那样简单,如果您在服务器端方法处理请求后向客户端发送响应,则只需要@ResponseBody.
RequestMappingHandlerAdapter是Spring框架的新映射处理程序,它自Spring 3.1起取代了AnnotationMethodHandlerAdapter.如果您的现有配置仍在AnnotationMethodHandlerAdapter中,您可能会发现此帖子很有用.我的帖子中提供的配置将让您了解如何设置RequestMappingHandlerAdapter.
您需要设置一个消息转换器.这是您的序列化JSON消息体在服务器端转换为本地Java对象的方式.
基本配置从这里开始.转换器是MarshallingHttpMessageConverter和CastorMarshaller的基本配置示例,我已经用MappingJackson2HttpMessageConverter和MappingJacksonHttpMessageConverter替换它们.
我的项目设置方式,我有两个配置文件:
hadlerAdapter bean必须位于MVC Dispatcher XML文件的后面.
<bean name="handlerAdapter"
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
<ref bean="jsonConverter"/>
</list>
</property>
<property name="requireSession" value="false"/>
</bean> …
Run Code Online (Sandbox Code Playgroud) 我创建了一些jsp文件,作为响应返回一些json字符串.但我发现Content-Type自动设置为txt
我的jsp代码看起来像
<%@ page import="java.util.Random" %>
<%@ page language="java" %>
<%@ page session="false" %>
<%
String retVal = "// some json string";
int millis = new Random().nextInt(1000);
// System.out.println("sleeping for " + millis + " millis");
Thread.sleep(millis);
%>
<%=retVal%>
Run Code Online (Sandbox Code Playgroud)
我怎样才能表现出类似的东西
setHeader("Content-Type", "application/json");
Run Code Online (Sandbox Code Playgroud)
在这个例子中?
我正在使用JSP生成动态页面,我想将此动态生成的完整页面保存为文件存档.
在JSP中,所有内容都写入 PrintWriter out = response.getWriter();
在页面的末尾,在向客户端发送响应之前,我想要保存此页面,无论是在文件中还是在缓冲区中作为字符串,以便以后处理.
如何保存Printwriter
内容或转换为String
?
我过去几个小时一直在摆弄这段代码片段,但我无法理解jQuery的自动完成UI是如何工作的.我按照本教程http://viralpatel.net/blogs/tutorial-create-autocomplete-feature-with-java-jsp-jquery/ 我使用了相同的示例,但我没有向JSP发送请求,而是使用了servlet.请求到达servlet"Fetcher",它也会执行,但没有任何内容返回到页面.这是代码.
public class Fetcher extends HttpServlet {
[...]
List<String> countryList = new ArrayList<String>();
String param = request.getParameter("term");
countryList.add("USA");
countryList.add("Pakistan");
countryList.add("Britain");
countryList.add("India");
countryList.add("Italy");
countryList.add("Ireland");
countryList.add("Bangladesh");
countryList.add("Brazil");
countryList.add("United Arab Emirates");
PrintWriter out = response.getWriter();
response.setContentType("text/plain");
response.setHeader("Cache-Control", "no-cache");
for(String country : countryList){
out.println(country);
}
[...]
}
Run Code Online (Sandbox Code Playgroud)
HTML中的Javascript片段:
<script>
$(function() {
$( "#tags" ).autocomplete({
source: "Fetcher"
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
HTML表单:
<label for="tags">Tags: </label>
<input id="tags" />
Run Code Online (Sandbox Code Playgroud)
页面上的示例似乎是在jquery中为专业人士编写的, http://jqueryui.com/autocomplete/#default.请,有人可以告诉它究竟是如何工作的,以便我可以在其他地方使用它.
我的目标是使用URLConnection将客户端应用程序中的对象发送到服务器对象用户:
Public class user {
String nom;
Integer id ;
boolean sex;
}
Run Code Online (Sandbox Code Playgroud)
我不希望逐字段发送它,而是作为对象.
我在Header中发送了JWT的令牌,但是客户端在响应的主体中需要它,如何将其放在响应中:
@Override
受保护的无效successAuthentication(
HttpServletRequest请求,HttpServletResponse响应,FilterChain链,身份验证authResult)
引发IOException,ServletException {
User springUser = (User) authResult.getPrincipal();
String jwt = Jwts.builder()
.setSubject(springUser.getUsername())
.setExpiration(new Date(System.currentTimeMillis()+SecurityConstants.EXPIRATION_TIME))
.signWith(SignatureAlgorithm.HS256, SecurityConstants.SECRET)
.claim("roles",springUser.getAuthorities())
.compact();
response.addHeader(SecurityConstants.HEADER_STRING, SecurityConstants.TOKEN_PREFIX+jwt);
}
Run Code Online (Sandbox Code Playgroud)
我需要将令牌放入响应中
java ×7
jsp ×3
servlets ×3
content-type ×2
http-headers ×2
json ×2
ajax ×1
curl ×1
html-post ×1
http ×1
javascript ×1
jquery ×1
jwt ×1
printwriter ×1
spring ×1
spring-boot ×1