Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java CXF使用JSON调用REST服务_Java_Json_Spring_Rest_Cxf - Fatal编程技术网

Java CXF使用JSON调用REST服务

Java CXF使用JSON调用REST服务,java,json,spring,rest,cxf,Java,Json,Spring,Rest,Cxf,我有一个小问题,这是因为我在REST和JSON方面的经验不太好 但是我有这项服务 @Path("/baseService") @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_XML}) @Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML,MediaType.APPLICATION_XML}) public interface BaseService {

我有一个小问题,这是因为我在REST和JSON方面的经验不太好

但是我有这项服务

 @Path("/baseService")
 @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
 @Consumes({MediaType.APPLICATION_JSON,       MediaType.TEXT_XML,MediaType.APPLICATION_XML})
 public interface BaseService {

 @POST
 @Path("/registration")
 @Transactional
 public UserTO register(@RequestBody UserTO userto);
我想从clent发送一个json来测试服务

如果我在没有json的情况下给他打电话,一切正常:

 resp=client.post(userTO);
但我不知道如何将其称为发送和json(让spring jackson为您将其从json转换为UserToObject),在线阅读我尝试的一些解决方案:

client.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).query("mail", "test").query("name", "test").query("psw", "test").query("role", "cassa").query("surname", "fsddsf").query("userName", "fsdfs").post(UserTO.class);


我该怎么做?请帮忙

您可以将json作为jsonobject(
jsonobject.toString()
)传递给为服务端点创建的输出流

URL url = new URL("http://localhost:8080/yourapp/baseService/registration");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(jsonObject.toString());
out.close();

BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));

如果您只是想测试您的应用程序,那么在这种情况下,您可以使用用于chrome的postman插件


Jakson拥有ObjectMapper类,该类将帮助您将java obj转换为pojo或从pojo转换而来。

以这种方式解决,更改

 resp=client.accept(MediaType.APPLICATION_JSON).post(inputJsonObj);
与:


传递JSON对象和我要将其转换为的类

新建InputStreamReader(connection.getInputStream()
会丢失字符集。对于JSON,字符集应始终为UTF-8,因此
新建InputStreamReader(connection.getInputStream(),StandardCharsets.UTF-8)
better@artbristol…谢谢,根据建议更新了答案。就我而言,答案是
UTF_8
,而不是
UTF-8
 resp=client.accept(MediaType.APPLICATION_JSON).post(inputJsonObj);
 users = client.post(inputJsonObj, UserTO.class);