RestEasy服务为浏览器请求和Java客户端请求返回不同的响应
我写了一个resteasy spring服务:RestEasy服务为浏览器请求和Java客户端请求返回不同的响应,java,web-services,resteasy,Java,Web Services,Resteasy,我写了一个resteasy spring服务: @Controller @RequestMapping("/abr/asd") @Path("/ab/ac") public class JobSchedulerService { private static final Logger LOGGER = Logger.getLogger(JobSchedulerService.class); @GET @Path(
@Controller
@RequestMapping("/abr/asd")
@Path("/ab/ac")
public class JobSchedulerService {
private static final Logger LOGGER = Logger.getLogger(JobSchedulerService.class);
@GET
@Path("/abc/{param}")
public String scheduleJobs(@PathParam("param") String name) {
return "Success";
}
}
try {
URL url = new URL("http://localhost:8080/WebApp/rest/ab/ac/abc/name");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new RuntimeException("Failed! " + conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String output;
System.out.println("Output from Server is: ");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
当我尝试从浏览器调用此服务时,响应正常:
http://localhost:8080/ControlAppWeb/rest/ab/ac/abc/name
成功
@Controller
@RequestMapping("/abr/asd")
@Path("/ab/ac")
public class JobSchedulerService {
private static final Logger LOGGER = Logger.getLogger(JobSchedulerService.class);
@GET
@Path("/abc/{param}")
public String scheduleJobs(@PathParam("param") String name) {
return "Success";
}
}
try {
URL url = new URL("http://localhost:8080/WebApp/rest/ab/ac/abc/name");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new RuntimeException("Failed! " + conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String output;
System.out.println("Output from Server is: ");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
请帮忙,我从Java API调用时哪里出错了
进一步:我尝试从服务中删除控制器和请求映射注释,但浏览器调用仍然有效,而JavaREST客户端没有。奇怪的是,即使我将url修改为任何随机的内容(在java客户端中),也不包括初始部分(http://localhost:8080/ControlAppWeb),输出保持不变,不会抛出错误…本例中的问题似乎是由于控件应用程序的web.xml中配置的“过滤器”引起的。因此,对/*的所有请求都被重定向到欢迎页面。从web.xml文件中删除过滤器配置解决了问题,Java Rest API的响应与浏览器调用的响应“成功”相同。虽然我仍然不清楚为什么过滤器不会影响来自浏览器的请求,但会影响来自Java客户端的请求。
无论如何,希望这能帮助其他可能遇到类似问题的人。在浏览器中检查您的cookie,可能您已经登录,所以浏览器请求正常接受,而您的url连接未经验证yet@hoaz感谢您的建议…登录页面我指的是欢迎页面(srry表示混乱)…但问题仍然存在…我尝试从服务端删除Controller和RequestMapping注释,但没有任何效果…另外注意到的是,从java rest客户端调用时,无论我给出了什么url,除了初始部分(服务输出始终是相同的,即网页…从未出现错误…请建议。。。