2014-06-20 19 views
15

मैं वसंत-डेटा-आराम परियोजना में वसंत वैधकर्ताओं को जोड़ने की कोशिश कर रहा हूं।स्प्रिंग-डेटा-रेस्ट वैलिडेटर

मैं साथ पीछा किया और सेटअप "शुरू हो रही है" इस लिंक के माध्यम से आवेदन: http://spring.io/guides/gs/accessing-data-rest/

... और अब मैं दस्तावेजों यहाँ का पालन करते हुए एक कस्टम PeopleValidator जोड़ने के लिए कोशिश कर रहा हूँ: http://docs.spring.io/spring-data/rest/docs/2.1.0.RELEASE/reference/html/validation-chapter.html

मेरे कस्टम PeopleValidator लग रहा है अब इस

की तरह लग रहा

तरह
package hello; 

import org.springframework.validation.Errors; 
import org.springframework.validation.Validator; 

public class PeopleValidator implements Validator { 
    @Override 
    public boolean supports(Class<?> clazz) { 
     return true; 
    } 

    @Override 
    public void validate(Object target, Errors errors) { 
     errors.reject("DIE"); 
    } 
} 

... और मेरे Application.java वर्ग

package hello; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.Import; 
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration; 

@Configuration 
@EnableJpaRepositories 
@Import(RepositoryRestMvcConfiguration.class) 
@EnableAutoConfiguration 
public class Application { 

    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 

    @Bean 
    public PeopleValidator beforeCreatePeopleValidator() { 
     return new PeopleValidator(); 
    } 
} 

मुझे उम्मीद है कि http://localhost:8080/people यूआरएल पर पोस्ट करने से यूआरएल कुछ प्रकार की त्रुटि होगी क्योंकि पीपुल्सवैलिएटर सबकुछ अस्वीकार कर रहा है। हालांकि, कोई त्रुटि नहीं फेंक दी जाती है, और वैधकर्ता को कभी नहीं कहा जाता है।

मैंने स्प्रिंग-डेटा-बाकी दस्तावेज़ों की धारा 5.1 में दिखाए गए सत्यापनकर्ता को मैन्युअल रूप से सेट करने का भी प्रयास किया है।

मुझे क्या याद आ रही है?

उत्तर

11

तो यह पहले प्रकट होने वाला/"बचाने" की घटनाओं केवल रख दिया और PATCH पर आग के बाद। पोस्ट करते समय, घटनाओं को आग बनाने के पहले/बाद में आग लगती है।

मैंने इसे configureValidatingRepositoryEventListener ओवरराइड का उपयोग करके मैन्युअल तरीके से फिर से प्रयास किया और यह काम किया। मुझे यकीन नहीं है कि मैं घर पर काम से अलग तरीके से क्या कर रहा हूं। मुझे कल देखना होगा।

मुझे यकीन है कि दूसरों को यह सुझाव देना अच्छा लगेगा कि यह क्यों काम नहीं करेगा।

रिकॉर्ड के लिए, नया एप्लिकेशन.जावा वर्ग कैसा दिखता है।

package hello; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.Import; 
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 
import org.springframework.data.rest.core.event.ValidatingRepositoryEventListener; 
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration; 

@Configuration 
@EnableJpaRepositories 
@Import(RepositoryRestMvcConfiguration.class) 
@EnableAutoConfiguration 
public class Application extends RepositoryRestMvcConfiguration { 

    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 

    @Override 
    protected void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener validatingListener) { 
     validatingListener.addValidator("beforeCreate", new PeopleValidator()); 
    } 
} 
+0

स्पष्ट रूप से "फर्स्टसेव" ईवेंट आग नहीं लगाता है, लेकिन "पहले क्रेट" ईवेंट करता है। मुझे अभी भी "पहले क्रिएटिवोपॉलिडाइटर" नामक एक बीन का उपयोग करने के बजाय "मैनुअल" तरीका तार करना पड़ा। –

+0

मुझे एक ही स्थिति मिल रही है, ऑटो खोज काम नहीं कर रही है। – JBCP

+0

मुझे एक सिमिलियर समस्या भी है - मैन्युअल रूप से तारों की घटनाएं ठीक काम कर रही हैं, स्वचालित खोज विफल हो जाती है। – Mikk

1

अंधेरे में एक स्टैब का थोड़ा सा - मैंने spring-data-rest का उपयोग नहीं किया है। हालांकि, आपके द्वारा अनुसरण किए जा रहे ट्यूटोरियल को पढ़ने के बाद, मुझे लगता है कि समस्या यह है कि आपको PersonValidatorPeopleValidator नहीं चाहिए। सब कुछ तदनुसार नाम बदलें:

PersonValidator

package hello; 

import org.springframework.validation.Errors; 
import org.springframework.validation.Validator; 

public class PersonValidator implements Validator { 
    @Override 
    public boolean supports(Class<?> clazz) { 
     return true; 
    } 

    @Override 
    public void validate(Object target, Errors errors) { 
     errors.reject("DIE"); 
    } 
} 

आवेदन

package hello; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.Import; 
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration; 

@Configuration 
@EnableJpaRepositories 
@Import(RepositoryRestMvcConfiguration.class) 
@EnableAutoConfiguration 
public class Application { 

    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 

    @Bean 
    public PersonValidator beforeCreatePersonValidator() { 
     return new PersonValidator(); 
    } 
} 
+0

सुझाव के लिए धन्यवाद। लेकिन ऐसा कोई फर्क नहीं पड़ता। मैंने इसे मैन्युअल तरीके से पुनः प्रयास किया और यह काम किया। जब मैं काम करता हूं तो मुझे अपने प्रोजेक्ट पर फिर से प्रयास करना होगा। –

+0

@Muel - आपने इसका परीक्षण कैसे किया? –

2

लगता सुविधा की तरह वर्तमान में लागू नहीं है (2.3.0), unluckily वहाँ घटना नाम के लिए कोई स्थिरांक हैं अन्यथा नीचे समाधान है कि कमजोर नहीं होगा।

Configuration सही घटना का उपयोग कर Validator बीन्स ValidatingRepositoryEventListener पर ठीक से नामित करता है।

import gr.bytecode.restapp.model.Agent; 
import org.springframework.data.rest.core.annotation.HandleBeforeCreate; 
import org.springframework.data.rest.core.annotation.HandleBeforeSave; 
import org.springframework.data.rest.core.annotation.RepositoryEventHandler; 
import org.springframework.stereotype.Component; 

@Component 
@RepositoryEventHandler(Agent.class) 
public class AgentEventHandler { 

    public static final String NEW_NAME = "**modified**"; 

    @HandleBeforeCreate 
    public void handleBeforeCreates(Agent agent) { 
      agent.setName(NEW_NAME); 
    } 

    @HandleBeforeSave 
    public void handleBeforeSave(Agent agent) { 
     agent.setName(NEW_NAME + "..update"); 
    } 
} 

उदाहरण github के लिए संपादित से है:

import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 
import java.util.Map; 

import org.springframework.beans.factory.InitializingBean; 
import org.springframework.beans.factory.ListableBeanFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.data.rest.core.event.ValidatingRepositoryEventListener; 
import org.springframework.validation.Validator; 

@Configuration 
public class ValidatorRegistrar implements InitializingBean { 

    private static final List<String> EVENTS; 
    static { 
     List<String> events = new ArrayList<String>(); 
     events.add("beforeCreate"); 
     events.add("afterCreate"); 
     events.add("beforeSave"); 
     events.add("afterSave"); 
     events.add("beforeLinkSave"); 
     events.add("afterLinkSave"); 
     events.add("beforeDelete"); 
     events.add("afterDelete"); 
     EVENTS = Collections.unmodifiableList(events); 
    } 

    @Autowired 
    ListableBeanFactory beanFactory; 

    @Autowired 
    ValidatingRepositoryEventListener validatingRepositoryEventListener; 

    @Override 
    public void afterPropertiesSet() throws Exception { 
     Map<String, Validator> validators = beanFactory.getBeansOfType(Validator.class); 
     for (Map.Entry<String, Validator> entry : validators.entrySet()) { 
      EVENTS.stream().filter(p -> entry.getKey().startsWith(p)).findFirst() 
        .ifPresent(p -> validatingRepositoryEventListener.addValidator(p, entry.getValue())); 
     } 
    } 
} 
0

यह ऐसा करने का एक और तरीका है http://docs.spring.io/spring-data/rest/docs/2.1.0.RELEASE/reference/html/events-chapter.html#d5e443

यहाँ कैसे एनोटेट संचालकों का उपयोग करने का एक उदाहरण है को यहां निर्धारित एनोटेट संचालकों उपयोग करने के लिए है संक्षिप्तता।

संबंधित मुद्दे