Spring RestController/Angular nie odczytuje z http scope

Odpowiedz Nowy wątek
2017-01-09 12:15
0

Controller

java

package com.martin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Component
@RestController
@RequestMapping("employee")
public class EmployeeController {

    public final EmployeeRepository employeeRepository;

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    public EmployeeController(EmployeeRepository employeeRepository) {
        this.employeeRepository = employeeRepository;
    }

    @GetMapping
    public Iterable<Employeer> getTalks() {
        return employeeRepository.findAll();

    }
}

Plik html z angularem

html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>ziom</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="people">
    <table>
        <tr ng-repeat="x in names">
            <td>{{ x.firstName }}</td>
            <td>{{x.salary}}</td>

        </tr>
    </table>

</div>

<script>
    var app = angular.module('myApp', []);

    app.controller('people', function($scope, $http) {
        $http.get('http://localhost:8080/employee/')
                .success(function (response) {$scope.names =response;});
    });

</script>

</body>
</html>

naforum.png

plik html z takimi samymi jsonami co na powyższym zdjęciu

html

[
{
"id": 1,
"salary": 0,
"firstName": null,
"lastName": null,
"address": null,
"phones": []
},
{
"id": 2,
"salary": 500,
"firstName": "Jan",
"lastName": "Sobieski",
"address": null,
"phones": []
},
{
"id": 3,
"salary": 500,
"firstName": "Jan",
"lastName": "Sobieski",
"address": null,
"phones": []
},
{
"id": 4,
"salary": 0,
"firstName": null,
"lastName": null,
"address": null,
"phones": []
}
]

Witam, nie wiem co jest nie tak z tym moim controllerem...
Jak w angularze mam " $http.get('http://localhost:8080/employee/')"
to nic się nie dzieje, brak reakcji ( jakby adres zły był [ bo pewnie jest ] )
a jak wpisze cokolwiek innego w $http.get('.....') czy to będzie ten plik html z danymi, lub jaki kolwiek inny z internetu adres http z jsonami to wszystko się dobrze wyświetla

Pozostało 580 znaków

2017-01-09 12:28
0

A co pokazuje konsola JS ?

jaka konsola JS :P? - Pes2009 2017-01-09 12:40
W chromie nazywa się to "Narzędzia dla programistów" (Ctrl + Shift + j), w FF "Konsola WWW" jak się nie mylę. - Adawo 2017-01-09 12:48
XMLHttpRequest cannot load http://localhost:8080/employee/employee. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access. - Pes2009 2017-01-09 12:53

Pozostało 580 znaków

2017-01-09 12:29
0
  1. RestController to jest już Component, nie musisz powielać adnotacji bez potrzeby
  2. W mappingu dałbym na wszelki wypadek "/employee"
  3. A co się dzieje jak z przeglądarki otworzysz sobie ten adres?
  4. Pokaż log ze startu aplikacji, spring loguje tam urle które mapuje

Masz problem? Pisz na forum, nie do mnie. Nie masz problemów? Kup komputer...

Pozostało 580 znaków

2017-01-09 12:35
0

tak jak w postmanie.

screenshot-20170109123108.png

logi

. _ _
/\ /
'_ () \ \ \ \
( ( )\
| ' | '| | ' \/ ` | \ \ \ \
\/ _)| |)| | | | | || (| | ) ) ) )
' |__
| .|| ||| |\, | / / / /
=========|_|==============|__/=////
:: Spring Boot :: (v1.4.1.RELEASE)

2017-01-09 11:04:05.022 INFO 6500 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$b4be6e75] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-01-09 11:04:05.591 INFO 6500 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-01-09 11:04:05.606 INFO 6500 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-01-09 11:04:05.607 INFO 6500 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.5
2017-01-09 11:04:05.740 INFO 6500 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-01-09 11:04:05.741 INFO 6500 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2785 ms
2017-01-09 11:04:05.899 INFO 6500 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-01-09 11:04:05.903 INFO 6500 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/]
2017-01-09 11:04:05.904 INFO 6500 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/
]
2017-01-09 11:04:05.904 INFO 6500 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/]
2017-01-09 11:04:05.904 INFO 6500 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/
]
2017-01-09 11:04:06.195 INFO 6500 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2017-01-09 11:04:06.216 INFO 6500 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2017-01-09 11:04:06.306 INFO 6500 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.0.11.Final}
2017-01-09 11:04:06.308 INFO 6500 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2017-01-09 11:04:06.310 INFO 6500 --- [ main] org.hibernate.cfg.Environment : HHH000021: Bytecode provider name : javassist
2017-01-09 11:04:06.364 INFO 6500 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2017-01-09 11:04:07.332 INFO 6500 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2017-01-09 11:04:07.999 INFO 6500 --- [ main] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000227: Running hbm2ddl schema export
Hibernate: alter table employeer drop foreign key FKr003lq531f5j0e3n98qu9ys7s
Hibernate: alter table phone drop foreign key FKp6j9el1bbg4ocl1dha1hq4c0y
Hibernate: drop table if exists account
Hibernate: drop table if exists address
Hibernate: drop table if exists employeer
Hibernate: drop table if exists phone
Hibernate: create table account (id bigint not null auto_increment, balance integer not null, owner_name varchar(255), primary key (id))
Hibernate: create table address (id bigint not null auto_increment, locality varchar(255), street varchar(255), street_number integer not null, zip_code varchar(255), primary key (id))
Hibernate: create table employeer (id bigint not null auto_increment, first_name varchar(255), last_name varchar(255), salary double precision not null, address_id bigint, primary key (id))
Hibernate: create table phone (id bigint not null auto_increment, number varchar(255), owner_id bigint, primary key (id))
Hibernate: alter table employeer add constraint FKr003lq531f5j0e3n98qu9ys7s foreign key (address_id) references address (id)
Hibernate: alter table phone add constraint FKp6j9el1bbg4ocl1dha1hq4c0y foreign key (owner_id) references employeer (id)
2017-01-09 11:04:10.470 INFO 6500 --- [ main] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000230: Schema export complete
2017-01-09 11:04:10.519 INFO 6500 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2017-01-09 11:04:11.360 INFO 6500 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot[email protected]291ae: startup date [Mon Jan 09 11:04:02 CET 2017]; root of context hierarchy
2017-01-09 11:04:11.443 INFO 6500 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/account/withdraw/{id}/{ammo}],methods=[POST]}" onto public void com.martin.AccountController.withdraw(java.lang.Long,int)
2017-01-09 11:04:11.444 INFO 6500 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/account/{id}],methods=[GET]}" onto com.martin.Account com.martin.AccountController.findAccountById(java.lang.Long)
2017-01-09 11:04:11.444 INFO 6500 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/account/dodaj/{id}/{ammo}],methods=[POST]}" onto public void com.martin.AccountController.dodaj(java.lang.Long,int)
2017-01-09 11:04:11.445 INFO 6500 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/account/friendsprofile],produces=[application/json]}" onto java.util.List<com.martin.account> com.martin.AccountController.findAll()
2017-01-09 11:04:11.445 INFO 6500 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/account/add],methods=[POST]}" onto public void com.martin.AccountController.addAccount(com.martin.Account)
2017-01-09 11:04:11.445 INFO 6500 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/address/add],methods=[POST]}" onto public void com.martin.AddressController.saveAddress(com.martin.Address)
2017-01-09 11:04:11.448 INFO 6500 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/employee/delete/{id:\d+}],methods=[DELETE]}" onto void com.martin.EmployeeController.delete(java.lang.Long)
2017-01-09 11:04:11.449 INFO 6500 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/employee/{id}/phones/{phoneId:\d+}],methods=[GET]}" onto com.martin.Phone com.martin.EmployeeController.findCompanyEmployeeWithId(long,long)
2017-01-09 11:04:11.449 INFO 6500 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/employee/salary/{salary}],methods=[GET]}" onto java.util.List<com.martin.employeer> com.martin.EmployeeController.findBySalaryGreaterThan(double)
2017-01-09 11:04:11.449 INFO 6500 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/employee/add],methods=[POST]}" onto public void com.martin.EmployeeController.saveEmployeer(com.martin.Employeer)
2017-01-09 11:04:11.449 INFO 6500 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/employee],methods=[GET]}" onto public java.lang.Iterable<com.martin.employeer> com.martin.EmployeeController.getTalks()
2017-01-09 11:04:11.449 INFO 6500 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/employee/{firstName:[a-zA-Z]+}],methods=[GET]}" onto java.lang.Iterable<com.martin.employeer> com.martin.EmployeeController.findByFirstName(java.lang.String)
2017-01-09 11:04:11.449 INFO 6500 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/employee/lastname/{lastName:[a-zA-Z]+}],methods=[GET]}" onto java.lang.Iterable<com.martin.employeer> com.martin.EmployeeController.findByLastName(java.lang.String)
2017-01-09 11:04:11.451 INFO 6500 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/employee/{id:\d+}],methods=[GET]}" onto com.martin.Employeer com.martin.EmployeeController.findOne(java.lang.Long)
2017-01-09 11:04:11.451 INFO 6500 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/employee/list/{firstName}],methods=[GET]}" onto java.util.List<com.martin.employeer> com.martin.EmployeeController.countByFirstName(java.lang.String)
2017-01-09 11:04:11.452 INFO 6500 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/employee/count/{salary}],methods=[GET]}" onto java.util.List<com.martin.employeer> com.martin.EmployeeController.countBySalary(double)
2017-01-09 11:04:11.453 INFO 6500 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/phone],methods=[GET]}" onto public java.lang.Iterable<com.martin.phone> com.martin.PhoneController.getPhones()
2017-01-09 11:04:11.453 INFO 6500 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/phone/adds],methods=[POST]}" onto public void com.martin.PhoneController.savePhone(com.martin.Phone)
2017-01-09 11:04:11.455 INFO 6500 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-01-09 11:04:11.455 INFO 6500 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-01-09 11:04:11.488 INFO 6500 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-01-09 11:04:11.488 INFO 6500 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/
] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-01-09 11:04:11.537 INFO 6500 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-01-09 11:04:11.805 INFO 6500 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-01-09 11:04:11.977 INFO 6500 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-01-09 11:04:12.000 INFO 6500 --- [ main] com.martin.MartinApplication : Started MartinApplication in 9.991 seconds (JVM running for 10.528)
2017-01-09 11:04:35.498 INFO 6500 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-01-09 11:04:35.498 INFO 6500 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2017-01-09 11:04:35.518 INFO 6500 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 20 ms
2017-01-09 11:04:35.607 INFO 6500 --- [nio-8080-exec-1] o.h.h.i.QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory

Pozostało 580 znaków

2017-01-09 13:01

Problemem jest próba dostępu do zasobu z innej domeny z perspektywy aplikacji angularowej:

XMLHttpRequest cannot load http://localhost:8080/employee/employee. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access. 

Tu masz więcej informacji na ten temat:

dzięki, dodałem w kontrolerze @CrossOrigin(origins = "http://localhost:63342") i działa :) - Pes2009 2017-01-09 13:28

Pozostało 580 znaków

Odpowiedz
Liczba odpowiedzi na stronę

1 użytkowników online, w tym zalogowanych: 0, gości: 1, botów: 0