当我厌倦了jsontest.com正在获得正确的价值
getCurrentTime()
{
//return this.http.get('http://date.jsontest.com')
return this.http.get('http://localhost:8080/webserviceangular2/services/updateUserRecords/')
.map(res=>res.json());
}
import {Component} from 'angular2/core';
import {FormExample} from "./angular-typescript-form.component";
import {Jsonp, URLSearchParams} from 'angular2/http';
@Component({
selector: 'my-app',
template: `<angular-typescript-form>loading...</angular-typescript-form> <br>
<p>{{postData}}</p>
<button (click)="OnTestGet()">Test_get_request</button>
<p>{{getData |json}}</p>
`,
directives: [FormExample],
providers: [FormExample,JSONP_PROVIDERS]
})
export class FormComponent {
constructor(private httpservice: FormExample){}
getData: string;
OnTestPost(){
this.httpservice.postJSON()
}
OnTestGet()
{
this.httpservice.getCurrentTime()
.subscribe(
data=>console.log(this.getData=data),
error=>console.log(JSON.stringify(error)),
()=> console.log("finished"));
}
}
@Path("/updateManager")
public class UpdateManager {
@GET
@Produces("text/plain")
@Consumes("text/plain")
//@Produces("application/json")
//@Consumes("application/json")
@Path("/updateUserRecords/")
public Response updateUserRecords(){
try {
dbConnection = getDBConnection();
System.out.println("connected is created "+dbConnection);
myStatement = dbConnection.prepareStatement("select * from account");
System.out.println(myStatement);
rs = myStatement.executeQuery();
while (rs.next()) {
data=rs.getString(3);
}
ResponseBuilder rb=Response.status(200);
rb.cacheControl(cc);
rb.entity(data);
response=rb.build();
}catch (Exception e) {
System.out.println(e);
}
return response;
}
我在java restful webservice中获得了有效负载值,但客户端出现错误,有人可以帮助我吗?
答案 0 :(得分:0)
我认为您发送文字作为回应更改了您的@consume
和@produces
@Consumes(MediaType.APPLICATION_JSON),@Produces(MediaType.APPLICATION_JSON)
它可以通过JSON发送回复。
答案 1 :(得分:0)
谢谢,实际上没有变化,它会导致跨源问题,我在tomcat7 / conf / web.xml文件的最后一处添加了下面的代码。现在工作正常..
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.headers</param-name>
<param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
</init-param>
<init-param>
<param-name>cors.exposed.headers</param-name>
<param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
</init-param>
<init-param>
<param-name>cors.support.credentials</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cors.preflight.maxage</param-name>
<param-value>10</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>