springmvc怎么设置相应头信息

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springmvc怎么设置相应头信息相关的知识,希望对你有一定的参考价值。

参考技术A 我们知道response 的content type主要有:
text/html
text/plain
application/json;charset=UTF-8
application/octet-stream

先举一个例子,spring mvc中可以通过如下方式返回json字符串:
Java代码
@ResponseBody
@RequestMapping(value = "/upload")
public String upload(HttpServletRequest request, HttpServletResponse response,String contentType2)
throws IOException
String content = null;
Map map = new HashMap();
ObjectMapper mapper = new ObjectMapper();

map.put("fileName", "a.txt");
try
content = mapper.writeValueAsString(map);
System.out.println(content);
catch (JsonGenerationException e)
e.printStackTrace();
catch (JsonMappingException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();


return content;


虽然访问时返回的确实是json字符串,但是response 的content type是"

text/html

"这不是我们期望的,我们期望的response content type是"application/json"或者"application/json;charset=UTF-8",那么如何实现呢?
通过注解@RequestMapping 中的produces
用法如下:
Java代码
@RequestMapping(value = "/upload",produces="application/json;charset=UTF-8")
spring MVC官方文档:
Producible Media Types
You can narrow the primary mapping by specifying a list of producible media types. The request will be matched only if the Accept request header matches one of these values. Furthermore, use of the produces condition ensures the actual content type used to generate the response respects the media types specified in the producescondition. For example:
@Controller@RequestMapping(value = "/pets/petId", method = RequestMethod.GET, produces="application/json")@ResponseBodypublic Pet getPet(@PathVariable String petId, Model model) // implementation omitted

Just like with consumes, producible media type expressions can be negated as in !text/plain to match to all requests other than those with an Accept header value oftext/plain.
Tip

The produces condition is supported on the type and on the method level. Unlike most other conditions, when used at the type level, method-level producible types override rather than extend type-level producible types.

简单说下Retrofit怎么设置请求头信息

简单说下Retrofit怎么设置请求头信息

不多说,直接上代码
有三种方式:

1、直接在参数里写 每次访问的时候都要传入一下


@GET("weatherservice/citylist")
Observable<WeatherRestBean> queryWeather(@Header("apikey") String apikey,@Query("cityname") String cityname);

2、写到注解里这样就少了个参数,但是每定义个接口都要写一次也是比较麻烦

·
@Headers("apikey:ac7c302dc489a69082cbee6a89e3646c")
@GET("weatherservice/cityid")
Observable<WeatherEntity> query(@Query("cityid")String cityid);

3、在创建Retrofit的时候添加,最方便的方式

OkHttpClient client = new OkHttpClient.Builder()
            .addInterceptor(new Interceptor() 
            @Override
            public Response intercept(Chain chain) throws IOException 
                Request request = chain.request();
                Request.Builder builder1 = request.newBuilder();
                Request build = builder1.addHeader("apikey", "ac7c302dc489a69082cbee6********").build();
                return chain.proceed(build);
            
        ).retryOnConnectionFailure(true)
          .build();
mRetrofit = new Retrofit.Builder()
           .client(client)
           .baseUrl(ConstantApi.url)
           .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
           .addConverterFactory(GsonConverterFactory.create())

以上是关于springmvc怎么设置相应头信息的主要内容,如果未能解决你的问题,请参考以下文章

简单说下Retrofit怎么设置请求头信息

Android怎样在http头信息里设置参数

nodejs中发送https请求怎么设置请求头

id软件主页运用到页面之后版头怎么修改

fiddler断点测试修改响应指令有啥作用

如何调整针式打印机