Java 改装2-在带有车身的立柱上收到400个错误请求
我用的是改型2。 用body发送POST请求(作为JSON数组),但得到“400错误请求””。 同时,从SoapUI和我的http客户端(基于HttpURLConnection)发送请求可以正常工作 服务的API接口:Java 改装2-在带有车身的立柱上收到400个错误请求,java,json,retrofit2,Java,Json,Retrofit2,我用的是改型2。 用body发送POST请求(作为JSON数组),但得到“400错误请求””。 同时,从SoapUI和我的http客户端(基于HttpURLConnection)发送请求可以正常工作 服务的API接口: public interface ApiService { @POST( "let/fmir" ) @Headers( { "Accept-Encoding: gzip,deflate", "Content-Typ
public interface ApiService {
@POST( "let/fmir" )
@Headers( {
"Accept-Encoding: gzip,deflate",
"Content-Type: Application/Json;charset=UTF-8",
"Accept: Application/Json",
"User-Agent: Retrofit 2.3.0"
} )
Call<LetPOJO> fmir(
@Header( "Authorization" ) String authorization,
@Body String body
);
}
通过声明的API调用服务(输入数据被混淆):
及其答复:
HTTP/1.1 200 OK
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Type: application/json
Date: Wed, 24 Jan 2018 08:33:12 GMT
Content-Length: 689
[{"id":"L___1"}]
身体也变得模糊了。
我们可以看到内容类型:application/json
当我通过HttpURLConnection发送时,请求的HTTP头也是相同的,可以正常工作:
- 授权书
- 内容类型
- 接受
@Header
注释作为方法参数,但行为是相同的
这是服务器终结点:
@ApiOperation("Some description")
@POST @Path("fmir")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "ОК"),
@ApiResponse(code = 400, message = "some message"),
@ApiResponse(code = 500, message = "some message")
})
public List<Letter> getLet(
@ApiParam(value = "Authorization header")
@HeaderParam(HttpHeaders.AUTHORIZATION) final String authorization,
@Context ContainerRequestContext context,
final List<String> letterIds) {
// ...
}
@ApiOperation(“一些描述”)
@POST@Path(“fmir”)
@产生(MediaType.APPLICATION_JSON)
@使用(MediaType.APPLICATION_JSON)
@ApiResponses(值={
@ApiResponse(代码=200,信息=“öκ”),
@ApiResponse(代码=400,message=“some message”),
@ApiResponse(代码=500,message=“some message”)
})
公共列表getLet(
@ApiParam(value=“授权标头”)
@HeaderParam(HttpHeaders.AUTHORIZATION)最终字符串授权,
@Context ContainerRequestContext上下文,
最终列表(ID){
// ...
}
由于服务器端点需要一个字符串列表,我认为您的客户端也需要发送该列表(不发送字符串):
那么,试试这个:
public interface ApiService {
@POST( "let/fmir" )
@Headers( {
"Accept-Encoding: gzip,deflate",
"Content-Type: Application/Json;charset=UTF-8",
"Accept: Application/Json",
"User-Agent: Retrofit 2.3.0"
} )
Call<LetPOJO> fmir(
@Header( "Authorization" ) String authorization,
@Body List<String> body
);
}
...
ApiService apiService = ApiServiceProvider.createApiService();
Call<LetPOJO> call = apiService.fmir(
"Basic NDUhg4234OQ==",
Arrays.asList("L___1", "L___2", "L___3", "L___4")
);
公共接口服务{
@邮政(“let/fmir”)
@标题({
“接受编码:gzip,deflate”,
“内容类型:Application/Json;charset=UTF-8”,
“接受:应用程序/Json”,
“用户代理:改装2.3.0”
} )
呼叫fmir(
@标题(“授权”)字符串授权,
@主体列表主体
);
}
...
ApiService ApiService=ApiServiceProvider.createApiService();
Call Call=apiService.fmir(
“基本NDUhg4234OQ==”,
数组.asList(“L_uuuuu1”、“L_uuu2”、“L_uuu3”、“L_uuu4”)
);
您确定@Headers中的“Application/Json”可以吗?我会使用“application/json”作为内容类型,因为它通常是拼写的(在RFC中也是如此),我相信改型已经设置了内容类型头。您是否可以在不使用该方法上面的整个@Headers({…})
的情况下重试?另外,您能否发布api/v1.0/let/fmir
端点的服务器代码?(或者至少是它的方法签名)@DanielVoina,是的,我也试过用小写字母,调试改型执行-它忽略大小写。@BartKiers,1。“您能不能在不使用该方法上面的整个@Headers({…})
的情况下再试一次?”-我试过了-不幸的是,也是这样。2.我将尝试查找代码-这将需要一些时间。@BartKiers这里是方法签名:@ApiOperation(“一些描述”)@POST@Path(“fmir”)@products(MediaType.APPLICATION\u JSON)@Consumes(MediaType.APPLICATION\u JSON)@ApiResponses(value={@ApiResponse(code=200,message=“öκ”),@apirresponse(code=400,message=“some message”),@ApiResponse(code=500,message=“some message”)})public List getLet(@ApiParam(value=“Authorization header”)@HeaderParam(HttpHeaders.Authorization)final String Authorization,@Context ContainerRequestContext,final List letterID){}
感谢您的参与。没问题@VitalyMorozov。它解决了你的问题吗?是的!感谢您的参与和建议。我试过了,结果很有帮助——现在得到了200个OK。它现在似乎可以工作了——我的响应中有一个JSON。谢谢!你也能看看我的问题吗?
POST http://1.1.1.1:3040/api/v1.0/let/fmir HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: application/json;charset=UTF-8
Authorization: Basic NDUhg4234OQ==
Content-Length: 129
Host: 1.1.1.1:3040
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
HTTP/1.1 200 OK
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Type: application/json
Date: Wed, 24 Jan 2018 08:33:12 GMT
Content-Length: 689
[{"id":"L___1"}]
@ApiOperation("Some description")
@POST @Path("fmir")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "ОК"),
@ApiResponse(code = 400, message = "some message"),
@ApiResponse(code = 500, message = "some message")
})
public List<Letter> getLet(
@ApiParam(value = "Authorization header")
@HeaderParam(HttpHeaders.AUTHORIZATION) final String authorization,
@Context ContainerRequestContext context,
final List<String> letterIds) {
// ...
}
public interface ApiService {
@POST( "let/fmir" )
@Headers( {
"Accept-Encoding: gzip,deflate",
"Content-Type: Application/Json;charset=UTF-8",
"Accept: Application/Json",
"User-Agent: Retrofit 2.3.0"
} )
Call<LetPOJO> fmir(
@Header( "Authorization" ) String authorization,
@Body List<String> body
);
}
...
ApiService apiService = ApiServiceProvider.createApiService();
Call<LetPOJO> call = apiService.fmir(
"Basic NDUhg4234OQ==",
Arrays.asList("L___1", "L___2", "L___3", "L___4")
);